标签: filereader

JAVA中的FileReader方法read()和read(char[])

FileReader我正在使用read()和方法从带有对象的文件中读取所有字符read(char[] ch)。但是当我尝试使用这两种方法时,我只得到其中一种方法的输出。

这是我的代码片段:

class FR
{
    void filereader() throws Exception
    {
        File f = new File("abc.txt");
        FileReader fr = new FileReader(f);
        char[] ch = new char[(int)f.length()];
        fr.read(ch);
        for (char ch1 : ch)
        {
            System.out.print(ch1);
        }
        System.out.println("\n*********************************");
        int i = fr.read();
        while(i != -1)
        {
            System.out.print((char)i);
            i = fr.read();
        }
        fr.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下为什么该while部分没有执行吗?

java file while-loop filereader java-io

0
推荐指数
1
解决办法
4103
查看次数

FileReader 方法不更新数据属性(vue.js)

我试图在vue.jsvia 中加载一个 JSON 文件FileReader。JSON 文件通过BoostrapVue表单文件输入作为 javascript File 对象实例加载。这是我当前的App.vue文件:

<template>
    <div v-if="fileUploaded">
        <div class="rf">
            <ChildComponent v-bind:json="json"/>
        </div>
    </div>
    <div v-else>
        <!-- BoostrapVueFileInput -->
    </div>
</template>

<script>
import ChildComponent from './ChildComponent.vue'

export default {
name: 'rf',
data () {
    return {
    fileUploaded: false,
    file: null,
    json: null
    }
},
methods: {
    read() {
    var reader = new FileReader();
    reader.onload = function(event) {
        this.json = JSON.parse(event.target.result);
    };
    reader.readAsText(this.file);
    }
}
}
</script>
Run Code Online (Sandbox Code Playgroud)

一旦我更新了 …

filereader fileapi vue.js

0
推荐指数
1
解决办法
1402
查看次数

文件阅读器打印出“?” 而不是文件

每当我点击编译时,我都会得到“?” 作为输出。没有错误或任何东西,只是问号。
这是我的代码:

import java.io.*;
    
public class FileReaderExample {
    
    public static void main(String[] args) {        
        try {
            FileReader fileReader = new FileReader("path to my text file");
            int data = fileReader.read();
            while (data != -1) {
                data = fileReader.read();
            }
            System.out.print((char)data); 
        } catch (Exception e) {
            System.err.println("There's been an error.");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

java filereader

0
推荐指数
1
解决办法
44
查看次数

使用 ifstream 从二进制文件一次读取 4 个字节

我有一个包含浮点数的二进制文件,因此每 4 个字节都是一个浮点数。我不确定如何以每四个字节存储为浮点数的方式读取,这样我就可以用它做任何我需要的事情。

这是我的代码:

int main()
{
    float i;
    std::ifstream inFile("bin_file", std::ios::binary);
    while (inFile >> i)
    {
        std::cout << i;
    }
    inFile.close();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,它甚至不会进入 while 循环,除非我将 i 定义为字符。我猜这是因为它每次读取 1 个字节并且无法将其存储为浮点数。顺便说一句,我已经检查过并且文件打开了。

谢谢!

c++ binaryfiles filereader

0
推荐指数
1
解决办法
4328
查看次数

在String line = x.readLine()中收到错误;

我在文件阅读方面遇到了一些问题

这是代码

FileReader fr = new FileReader ("products.txt"); {
String line = fr.readLine();

while(line != null){
    System.out.println(line);
}
Run Code Online (Sandbox Code Playgroud)

NetBeans给我一个错误

String line = fr.readLine();

特别是在readLine()下;

我试过nextLine,但也没用.

感谢您的帮助

java io netbeans filereader

-1
推荐指数
1
解决办法
1115
查看次数

静态属性上的NullPointerException

我在java中创建一个简单的登录程序.这是我到目前为止的代码.

import java.util.Scanner;
import java.io.*;
import java.util.Arrays;

public class PasswordProgram {

    public static String user;
    public String password;
    public static boolean part1Finish = false;
    public File file = new File("D:/file.txt");
    public FileWriter UsernameWrite;
    public char[] user1;

    public void part1() {
        System.out.println("Please create an account: ");
        Scanner input = new Scanner(System. in );
        System.out.println("Type in a username: ");
        String user = input.next();

        System.out.println("Type in a Password: ");
        String password = input.next();
        try {
            UsernameWrite = new FileWriter(file);
            UsernameWrite.write(user);
            UsernameWrite.write(password);
            System.out.println(user);
            UsernameWrite.close(); …
Run Code Online (Sandbox Code Playgroud)

java arrays filereader

-1
推荐指数
1
解决办法
71
查看次数

如何使用 JavaFX GUI 应用程序显示 .txt 文件中的数据

我想使用 JavaFX 显示 .txt 报告文件中的数据。在我的代码中,我尝试使用标签和 Vbox 在 GUI 中以多种格式显示信息到场景。但是,我将结果输出为 GUI 而不是控制台,结果很糟糕。我试图研究我的问题,但找不到解决问题所需的信息。

这是我需要使用 JavaFX 显示为 GUI 应用程序的报告:

来自控制台的图像而不是来自 GUI

这是我的代码作为 GUI 显示的内容:

运行 GUI 时的图像

这是我的源代码:


package sample;

public class CustomerSale {

    // Details of reports
    private String zipCodeExtension;

    private int customerNumber;

    private String customerName;

    private int purchaseDate;

    private String make;

    private int purchasePrice;

    private int yearOfVehicle;

    private int satisfactionRating;


    // Create constructor argument

    public CustomerSale(String zipCodeExtension, int customerNumber, String customerName,
                        int purchaseDate, String make, int purchasePrice,
                        int yearOfVehicle, int satisfactionRating) {
        this.zipCodeExtension = zipCodeExtension;
        this.customerNumber …
Run Code Online (Sandbox Code Playgroud)

java javafx filereader bufferedreader

-1
推荐指数
1
解决办法
101
查看次数

读取文件并以相反的顺序写入另一个文件,并删除"a"和"the"

我想编写一个程序,使用C#读取一个文件,并以相反的顺序吐出另一个原始文件中包含所有单词的文件,并删除所有单词,如"a"和"the".

using (FileStream stream = File.OpenRead("C:\\file1.txt"))
using (FileStream writeStream = File.OpenWrite("D:\\file2.txt"))
{
    BinaryReader reader = new BinaryReader(stream);
    BinaryWriter writer = new BinaryWriter(writeStream);

    // create a buffer to hold the bytes 
    byte[] buffer = new Byte[1024];
    int bytesRead;

    // while the read method returns bytes
    // keep writing them to the output stream
    while ((bytesRead =
            stream.Read(buffer, 0, 1024)) > 0)
    {
        writeStream.Write(buffer, 0, bytesRead);
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经实现了以前的代码.如何反转和吐出字符"a"和"the".

c# streamreader filereader memory-mapped-files

-2
推荐指数
1
解决办法
825
查看次数

java do while循环无法访问变量

我在java中编写一个简单的文件I/O程序,我的程序允许用户输入他们想要编辑的文件(这将是我使用我的FileReader读取的文件).但是,我想继续提示用户输入文件名,直到他们输入有效文件(已存在的文件).出于某种原因,我的编译器(Eclipse)告诉我在任何地方都没有使用"布尔变量loopFlag".

当我运行程序时,如果用户输入错误的输入(不存在的文件),程序将正确运行并再次提示用户输入文件名.但是,如果用户输入了一个好的输入(已存在的文件),程序将无限循环并仍然提示用户输入文件名.这是我的代码:

String fileName;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // read in user input
boolean loopFlag; // compiler says this is not used anywhere, but I use it in my do-while loop?!?!

do
{
     loopFlag = false;
     System.out.println("Please enter the file name you wish to edit (including extension):");
     fileName = br.readLine(); // set fileName equal to user input
     // this try catch block is to make sure that the file exists
     try
     {
          FileReader fr = new FileReader(ManipulateText.readFileName);
          fr.close(); …
Run Code Online (Sandbox Code Playgroud)

java variables file-io filereader do-while

-2
推荐指数
1
解决办法
76
查看次数

如何从文本文件中获取特定值

我有一个.txt文件:

80,90,100,110,120,130,140,150,160   
100,20,22,24,26,28,26,28,29,27   
110,30,32,34,36,37,39,37,39,40  
120,40,41,42,44,45,46,48,47,49
Run Code Online (Sandbox Code Playgroud)

表示百叶窗价格的表格,第一行是百叶窗的宽度,第一列没有80是高度.剩下的数字是价格.

我已经使用c#做了这个,但在java中我不知道该怎么做,在c#我的代码看起来像这样,一切都很好.有人能在java中向我展示同样的东西吗?theWidth和theHeight是我必须输入尺寸的文本字段.

                string[] lines = File.ReadAllLines(@"tabel.txt");
                string[] aux = lines[0].Split(',');
                for (int i = 0; i < aux.Length-1; i++)
                {
                    if (aux[i] == theWidth.ToString())
                    {
                        Console.WriteLine(aux[i]);
                        indiceLatime = i;
                    }
                }

                for (int i = 1; i < lines.Length; i++)
                {
                    aux = lines[i].Split(',');
                    if (aux[0] == theHeight.ToString())
                    {
                        showPrice.Text = aux[indiceLatime + 1];
                    }
                }
Run Code Online (Sandbox Code Playgroud)

在java中我尝试过这样的事情:

try {
        BufferedReader inputStream = new BufferedReader(new FileReader("tabel.txt"));
        int theWidth = 90;
        int theHeight = 100;
        int indiceLatime …
Run Code Online (Sandbox Code Playgroud)

java readline filereader bufferedreader readfile

-4
推荐指数
1
解决办法
6671
查看次数