我正在尝试将文件打印到文本文件。虽然我已经设法让它工作,但它只打印出控制台上打印的最后一行。例如,我的控制台大约有 8000 行,但它只打印出文本文件中的最后一行,我想将所有行打印到文本文件中。这是代码:
try {
BufferedReader reader = new BufferedReader(new FileReader("JDT.txt"));
Writer output = null;
File file = new File("output.txt");
output = new BufferedWriter(new FileWriter(file));
String line = reader.readLine();
int count=0;
while(line !=null)
{
for(int i = 0 ; i<faults.length;i++){
if(line.contains(faults[i]))
System.out.println(line);
count++;
output.write(line +"Total: "+count);
//System.out.println("File Written");
}
line=reader.readLine();
}
System.out.println("Printed Lines =" +count);
output.close();
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
提前谢谢你。