简体   繁体   English

使用同一个BufferedReader对象读取多个文件

[英]Using the same BufferedReader object to read more than one files

I am writing some code to parse all the files present in the current folder and subfolders. 我正在编写一些代码来分析当前文件夹和子文件夹中存在的所有文件。 It is able to read all the files but I am getting data only from the last file it reads, while I need data from all the files. 它能够读取所有文件,但我仅从读取的最后一个文件中获取数据,而我需要所有文件中的数据。 Any help on this will be much appreciated. 任何帮助,将不胜感激。 Below is the code format I am using: 以下是我使用的代码格式:

public static void scanLogs (String loc)throws IOException{
BufferedReader br= new BufferedReader (new FileReader (loc));
String line=br.readLine();
while(line!=null){
//process the input file
FileWriter fw = new FileWriter(WriteFileLoc.csv);
PrintWriter pw = new PrintWriter(fw);
pw.print();
line=br.readLine(); 
}
pw.flush();
pw.close();
fw.close();
br.close();
}

You need to open the file for appending: 您需要打开文件进行追加:

FileWriter fw = new FileWriter(WriteFileLoc.csv, true);

See: http://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html#FileWriter%28java.io.File%29 请参阅: http : //docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html#FileWriter%28java.io.File%29

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM