2014-01-16 66 views
0

我有這種方法列出目錄中的文件,我需要將它們寫入文件。顯然我的無法使用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文件,但文件是空的。

+0

嘗試'bw.flush();'了'for'循環 – Asfab

+0

後,此寫入只有最後的結果......但我已經在最近的到目前爲止:) – ErrorNotFoundException

+0

然後在'for'循環中使用'StringBuffer'並在其中追加值並在'for'循環退出時寫入它 – Asfab

回答

2

我認爲這是因爲你試圖通過遞歸調用自己時,打開多個緩衝作家到同一個文件。嘗試在此方法之外創建作者,並將其作爲參數傳遞。


public void myCallingMethod() throws IOException{ 
    File rep = new File("report.txt"); 
    BufferedWriter bw = new BufferedWriter(new FileWriter(rep)); 
    walk("my/path", 4, bw); 
    bw.close(); 
} 
+0

但我已經初始化BufferedWriter在遞歸for循環之外:P – ErrorNotFoundException

+0

不是,@Stanley。在遞歸中,您打開另一個文件。等等。 – Ingo

+2

@Stanley但是每次調用該方法時,新的BufferedWriter都會被初始化爲同一個文件。當它完成遞歸時,如果根目錄中的「路徑」只有一個目錄,它將不會寫入任何內容並將清空一個空文件。也許你應該在遞歸調用中傳遞BufferedWriter,以便所有遞歸調用都使用相同的編寫器實例。 – Saket

-1

您需要在關閉前執行

java.io.BufferedWriter#flush()

+0

即使使用Flush,它也不寫:( – ErrorNotFoundException

+0

您是否有任何大於1M的文件在你正在運行這個目錄?我注意到'if(size> = limit)'在我運行時過濾掉了所有的文件。有沒有長度大於1000000的文件? – Saket

+0

「BufferedWriter」在關閉時將收到「flush」。 http://docs.oracle.com/javase/7/docs/api/java/io/BufferedWriter.html#close() –

0

嘗試調用flush方法,你叫寫梅索德等之後:

bw.write("File:" + f.getAbsoluteFile() + " " + size + "MB Last Modified Date: " + sdf.format(f.lastModified()) + "\n"); 
bw.flush(); 

flush方法刷新您的流文件

+0

即使它不寫什麼:(見我的編輯 – ErrorNotFoundException

+0

那麼你應該檢查你寫的方法,也許你應該加一個偏移量和長度 [鏈接](http://docs.oracle。 COM/JavaSE的/ 7 /文檔/ API/JAVA/IO/BufferedWriter.html#寫(java.lang.String中,INT,INT)) – chuhx

+0

您還可以嘗試採取一個PrintWriter,而不是一個的BufferedWriter的 'PrintWriter的p =新PrintWriter的(新的FileWriter(REP));'' p.println(串);' – chuhx

0

下面是可能解決問題的代碼EM。 我也一樣。

public class example { 
    public static void main(String[] args) throws IOException { 

    // Directory path here 
    String path = "C:\\"; 
    SimpleDateFormat sdf = new SimpleDateFormat("MMM/dd/yyyy HH:mm:ss"); 

    File folder = new File(path); 
    File[] listOfFiles = folder.listFiles(); 
    File file = new File("report.txt"); 
    if (!file.exists()) { 
     file.createNewFile(); 
    } 
    FileWriter fw = new FileWriter(file.getAbsoluteFile()); 
    BufferedWriter bw = new BufferedWriter(fw); 
    long size; 
    int limit = 2; 
    for (File f : listOfFiles) { 
     size = f.length()/1000/1000; 

     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())); 
     } 

    } 
    bw.close(); 
} 

}

0

定義這樣

StringBuilder fileData = new StringBuilder(); 

一個變量,並更換

bw.write("File:" + f.getAbsoluteFile() + " " + size + "MB Last Modified Date: " + sdf.format(f.lastModified()) + "\n"); 
bw.flush(); 

隨着

fileData.append("File:").append(f.getAbsoluteFile()).append(" ").append(size) 
    .append("MB Last Modified Date: ").append(sdf.format(f.lastModified())) 
    .append("\n") ; 

而且for循環寫fileData後提交

bw.write(fileData.toString()); 
bw.close(); 
+0

您可以通過使用'StringBuilder'而不是'String'的提高,這也並不需要'flush'通話 –

+0

。這個收益只寫最後的結果 – ErrorNotFoundException

+0

謝謝@ViktorSeifert用'StringBuilder'修改了答案 – Asfab