我有這種方法列出目錄中的文件,我需要將它們寫入文件。顯然我的無法使用BufferedWriter寫入文件
System.out.println(); 能夠列出文件及其大小和它們被修改的日期。但是我的bufferedWriter不會在文件中寫任何東西。這是我的方法;
public void walk(String path, int limit) throws IOException {
File root = new File(path);
File[] list = root.listFiles();
File rep = new File("report.txt");
SimpleDateFormat sdf = new SimpleDateFormat("MMM/dd/yyyy HH:mm:ss");
if (list == null) return;
long size;
BufferedWriter bw = new BufferedWriter(new FileWriter(rep));
for (File f : list) {
size = f.length()/1000/1000;
if (f.isDirectory()) {
walk(f.getAbsolutePath(), limit);
}
else {
if(size >= limit){
System.out.println("File:" + f.getAbsoluteFile() + " " + size + "MB Last Modified Date: " + sdf.format(f.lastModified()));
bw.write("File:" + f.getAbsoluteFile() + " " + size + "MB Last Modified Date: " + sdf.format(f.lastModified()) + "\n");
}
}
}
bw.close();
}
我失蹤了什麼?我需要編寫Out to the file report.txt文件,但文件是空的。
嘗試'bw.flush();'了'for'循環 – Asfab
後,此寫入只有最後的結果......但我已經在最近的到目前爲止:) – ErrorNotFoundException
然後在'for'循環中使用'StringBuffer'並在其中追加值並在'for'循環退出時寫入它 – Asfab