PrintWriter不能在我的循环中工作,但我的循环正在工作

时间:2014-10-10 02:01:14

标签: java loops 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();
    }
  }

2 个答案:

答案 0 :(得分:1)

包括:

pr.flush();

在你的循环之后。

答案 1 :(得分:1)

使用flush()

pr.flush();

或创建autoflush PrintWriter

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