【问题标题】:PrintWriter won't work in my loops but my loops are workingPrintWriter 不能在我的循环中工作,但我的循环正在工作
【发布时间】:2014-10-10 02:01:14
【问题描述】:

它将在控制台中打印数组,因此我知道循环正在运行,但 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();
    }
  }

【问题讨论】:

    标签: java loops printwriter


    【解决方案1】:

    包括一个:

    pr.flush();
    

    就在你的循环之后。

    【讨论】:

    • 谢谢!不过我还没学过,有没有其他方法?
    • 你没学过是什么意思?它在官方文档中。
    【解决方案2】:

    使用flush()

    pr.flush();
    

    或创建自动刷新 PrintWriter

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-23
      • 1970-01-01
      • 1970-01-01
      • 2016-12-21
      • 2021-06-24
      • 2015-04-24
      • 1970-01-01
      相关资源
      最近更新 更多