我有一个包含1000个文件的文件夹。每个文件包含不同行数的文本。我想要和想要实现的是阅读每个'文件并将所有行追加到一行(也就是说,我希望每个文件都有一行文本)。
这是我试过的,但它只打印文件名而不影响文件的任何更改......
String line = "";
try{
file = new FileReader(filename);
BufferedReader reader = new BufferedReader (file);
while ((line = reader.readLine()) != null){
allLine.append(line);
}
//System.out.println(allLine);
} catch (IOException e) {
throw new RuntimeException("File not found");
}
return allLine.toString();
FileWriter op = null;
op = new FileWriter(fileName);
BufferedWriter wryt = new BufferedWriter(op);
wryt.write(s);
wryt.flush();
if(op != null){
op.close();
}
File[] lOfiles = folder.listFiles();
for (int i = 0; i< lOfiles.length; i++){
if(lOfiles[i].isFile()){
System.out.println(lOfiles[i].getName());
ReadLines rd = new ReadLines();
String rw = rd.readtxtFile(lOfiles[i].toString());
rd.writetxtFile(lOfiles[i].getName(), rw);
}
}
答案 0 :(得分:0)
try {
File folder = new File("yourfolderpath");
File out = new File("outputfile.txt");
try(BufferedWriter bw = new BufferedWriter(new FileWriter(out))){
for(File f: folder.listFiles()) {
BufferedReader br = new BufferedReader(new FileReader(f));
for(String line = br.readLine(); line!=null; line=br.readLine()) {
bw.write(line);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}