Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的BufferedReader不能正确读取我的文件?_Java_Bufferedreader_Bufferedwriter - Fatal编程技术网

Java 为什么我的BufferedReader不能正确读取我的文件?

Java 为什么我的BufferedReader不能正确读取我的文件?,java,bufferedreader,bufferedwriter,Java,Bufferedreader,Bufferedwriter,我有一个小程序,它从.txt文件中读取一个数字,增加它的数量,并在每次运行时将其打印回文件: public class ClientCounter { int count = 0; String input = ""; public int addClient() { try ( PrintWriter out = new PrintWriter(new FileWriter("test.txt"));

我有一个小程序,它从.txt文件中读取一个数字,增加它的数量,并在每次运行时将其打印回文件:

public class ClientCounter {

    int count = 0;
    String input = "";

    public int addClient() {

        try (
            PrintWriter out = new PrintWriter(new FileWriter("test.txt"));
            BufferedReader in = new BufferedReader(new FileReader("test.txt"));
        ) {
            input = in.readLine();

            System.out.println("Input line is " + input);

            if (input != null)
                count = Integer.parseInt(input);

            System.out.println("Client count is: " + count);
            count++;

            System.out.println("After increment, count is: " + count);

            String output = Integer.toString(count);
            System.out.println("Output is " + output);

            out.println(output);

        } catch(FileNotFoundException e) {
            System.out.println("FNFE!");
            System.exit(1);
        } catch(IOException e) {
            System.out.println("IOE!");
            System.exit(1);
        }

        return count;

    }
但是,每次我尝试运行它时,都会得到以下输出:

Input line is null
Client count is: 0
After increment, count is: 1
Output is 1
我的
test.txt
文件总是只包含一个数字,
1


知道为什么会发生这种情况吗?

每次重写新文件时

尝试如下,第二个参数(true)指示使用现有文件

 PrintWriter out = new PrintWriter(new FileWriter("test.txt",true));
上面的答案是错误的。更新了下面的答案

编辑的代码:可能是用户270349正确。下面的代码适用于我

公共类客户端计数器{

static int count = 0;
static String input = "";

public static void main(String args[]){
    addClient();
}

public static int addClient() {

    try {
        BufferedReader in = new BufferedReader(new FileReader("test.txt"));
        input = in.readLine();
        if (input != null)
            count = Integer.parseInt(input);
        System.out.println("Client count is: " + count);
        count++;
        in.close();

        System.out.println("After increment, count is: " + count);

        PrintWriter out = new PrintWriter(new FileWriter("test.txt"));
        String output = Integer.toString(count);
        System.out.println("Output is " + output);

        out.println(output);
        out.flush();out.close();

    } catch(FileNotFoundException e) {
        System.out.println("FNFE!");
        System.exit(1);
    } catch(IOException e) {
        System.out.println("IOE!");
        System.exit(1);
    }

    return count;
}
}

您没有关闭文件。当程序结束且缓冲区尚未写入磁盘时,您的修改将丢失。

您必须在处理完文件后立即关闭这些文件。将同一文件打开两次也是个坏主意。 也许你应该这样做:

BufferedReader in = ...
try {
  count = Integer.parseInt(in.readLine());
} finally {
  in.close();
}
count++;
  PrintWritter out = ...
try {
  out.println(count);
} finally {
  out.close();
}

用于从文件中读取某些内容

FileReader fr=新的FileReader(f)

BufferedReader br=新的BufferedReader(fr)

字符串str=null

而((str=br.readLine())!=null){

//行存储在“字符串s”中。可以使用str执行任何操作

}

br.close();//不要忘记通过调用close方法关闭BufferedReader的实例

用于在文件中写入某些内容

File f = new File("newfile.txt");
String content = "This is something I want to write";
BufferedWriter bw = new BufferedWriter(f);
bw.write(content);
bw.close();

我认为这是因为您读取了正在编写的文件。
PrintWriter out=new PrintWriter(new FileWriter(“test.txt”))创建一个空的新文件。文件可能没有准备好。当您试图读取它时。尝试使用
RandomAccessFile
关闭写入程序,并以读/写模式打开它?我在您的程序中看到的错误是,您不能同时打开同一个文件进行读写,这是串行操作,您需要请关闭一个以执行另一个。这可能不准确。文件在JVM退出时关闭。我见过很多初学者都会遇到这种情况。请在使用后关闭资源,否则您将变得不可预测(尽管在这种情况下是可预测的)结果。您应该使用try-finally或try-with-resources关闭资源。编辑后:您应该使用try-finally或try-with-resources关闭资源。使用代码,如果意外失败,文件可能会保持打开状态。例如
count=Integer.parseInt(input);
如果行不包含整数,则可能失败。
File f = new File("newfile.txt");
String content = "This is something I want to write";
BufferedWriter bw = new BufferedWriter(f);
bw.write(content);
bw.close();
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class BufferedReaderTest {


    public static void main(String args[]) {
         int count = 0;
         String input;

        try {
            BufferedReader br = new BufferedReader(new FileReader("E:\\this.txt"));
            PrintWriter pw = new PrintWriter(new FileWriter("E:\\this.txt"));
            input = br.readLine();
            if (input != null)
                count = Integer.parseInt(input);
            System.out.println("Client count is: " + count);

            System.out.println("After increment, count is: " + (++count));

            String output = Integer.toString(count);
            System.out.println("Output is " + output);

            pw.println(output);
            pw.flush();
            pw.close();

        } catch (FileNotFoundException e) {
            System.out.println("FNFE!");
            System.exit(1);
        } catch (IOException e) {
            System.out.println("IOE!");
            System.exit(1);
        }
    }
}