Java读/写文件-BufferedReader BufferedWriter

Java读/写文件-BufferedReader BufferedWriter,java,io,bufferedreader,bufferedwriter,Java,Io,Bufferedreader,Bufferedwriter,这是一段代码片段,但基本上我要做的是从名为“listings.txt”的文件中读取,然后写入名为“overview.txt”的文件。我想从“listings.txt”中提取信息,并按原样将其放入“overview.txt”中(稍后我会找出其余的内容) 文件“overview.txt”被创建,并在文件“listings.txt”中循环出现,然后写入“overview.txt”。但是,一旦我打开“overview.txt”文件,它就为空有人能快速浏览一下我的代码并发现错误吗 package yesO

这是一段代码片段,但基本上我要做的是从名为“listings.txt”的文件中读取,然后写入名为“overview.txt”的文件。我想从“listings.txt”中提取信息,并按原样将其放入“overview.txt”中(稍后我会找出其余的内容)

文件“overview.txt”被创建,并在文件“listings.txt”中循环出现,然后写入“overview.txt”。但是,一旦我打开“overview.txt”文件,它就为空
有人能快速浏览一下我的代码并发现错误吗

package yesOverview;

import java.io.BufferedReader;
import java.io.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;


public class yesOverview {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String strInput = "foo.bar";
        System.out.print("Please enter the listings file (the full path to the file): ");
        strInput = input.next();

        //This makes sure that the inputed file is listings.txt as required for KET1 task 2
        while (strInput.contains("listings.txt") == false) {
            System.out.print("Incorrect file. Please enter listings file(the full path to the file): ");
            strInput = input.next();
        }

        infos(strInput);
        input.close();
    }

    public static void infos(String strInput) {
        Scanner input2 = new Scanner(System.in);
        System.out.print("Please enter the overview.txt file (the full path to the file): ");
        String strInput2 = "foo.bar";
        strInput2 = input2.next();

        //This also makes sure that the overview.txt file is provided. 
        while (strInput2.contains("overview.txt") == false) {
            System.out.print("Incorrect file. Please enter overview file(the full path to the file): ");
            strInput2 = input2.next();
        }

        //Creates the file f then places it in the specified directory.
        File f = new File(strInput2);

        try {
            //Creates a printerwriter out that writes to the output file. 
            PrintWriter out = new PrintWriter(strInput2);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(KETTask2Overview.class.getName()).log(Level.SEVERE, null, ex);
        }

        //String that holds the value of the next line. 
        String inputLine = "";

        //Creates the Buffered file reader / writer.  
        try {
            BufferedReader in = new BufferedReader(new FileReader(strInput));
            FileWriter fstream = new FileWriter(strInput2);
            BufferedWriter out = new BufferedWriter(fstream);
            while (in.readLine() != null) {
                out.write(in.read());
            }
            in.close();
        } catch (IOException e) {
            System.err.println("Error: " + e.getMessage());
        }

    }
}
你没有结束。 writeList方法的finally块清除并关闭BufferedWriter

finally {
    if (out != null) { 
        System.out.println("Closing BufferedWriter");
        out.close(); 
    } else { 
        System.out.println("BufferedWriter not open");
    } 
} 
试试这个

关闭BufferedWriter流(即out.Close())

尝试使用nextLine()而不是next(),因为next()只接受一个单词,但对于完整的行,请使用nextLine(),尽管这似乎不是问题所在

当我必须读写文件时,我通常遵循以下步骤

用于从文件中读取

File f = new File("my.txt");
FileReader fr = new FileReader(f);
BufferedReader br  = new BufferedReader(fr);

String s = null;

while ((br.readLine())!=null) {

// Do whatever u want to do with the content of the file,eg print it on console using SysOut...etc

}

br.close();
要写入文件,请执行以下操作:

Boolean isDone = true;
Scanner scan = new Scanner(System.in);
File f = new File("my.txt");
FileWriter fr = new FileWriter(f);
BufferedWriter br  = new BufferedWriter(fr);

while (isDone) {

   if (!isDone) {

 br.write(new Scanner(System.in).nextLine());

 }


}
用法示例:

 copy( reader, new FileWriter( file ) );

您永远不会结束。@b参数是正确的-您必须结束。最佳做法是将关闭调用放在
finally{}
块中。@user949300或使用Java7中的语句。你们是对的。非常感谢。我把out.close()放进去,它现在就可以工作了。不是很完美,但我可以从这里找出其余的。。。谢谢当我了解到这一点时,我会回报社会@别忘了标记答案!整个问题是我没有结束它,尽管您提供的代码也非常有用。谢谢你的帮助!你是如何摆脱while(b)循环的,为什么要测试while(b)循环呢!b在它里面的时候,b在它里面永远不会是假的?这没有任何意义。是否需要这一行?:Scanner scan=新扫描仪(System.in);b从来没有定义过
 copy( reader, new FileWriter( file ) );