2014-10-10 20 views
1

它將在控制檯中打印數組,因此我知道循環正在工作,但PrintWriter不會在文件中打印任何內容。PrintWriter將不會在我的循環中工作,但我的循環正在工作

public static void writePuzzle(char[][] puzzle, String file){ 

    try{ 
     PrintWriter pr = new PrintWriter(new FileWriter ((file))); 

     for(int i= 0 ; i<puzzle.length; i++){ 
     for(int j= 0 ; j<puzzle.length; j++){ 

      pr.print(puzzle[i][j]); 
      System.out.print(puzzle[i][j]); 
     } 
     pr.println(); 
     System.out.println(""); 
     } 

    } catch (IOException e) { 

     e.printStackTrace(); 
    } 
    } 

回答

1

包括一個:

pr.flush(); 

只是你的循環之後。

+0

謝謝!但我還沒有明白,有沒有其他方法? – Ronbonbeno 2014-10-10 02:11:08

+0

你的意思是你沒有學到什麼?它在官方文檔中。 – 2014-10-10 02:21:59

1

使用沖水()

pr.flush(); 

或創建自動沖洗的PrintWriter

PrintWriter pw = new PrintWriter(..., true);