Ronbonbeno
Ronbonbeno

Reputation: 11

PrintWriter won't work in my loops but my loops are working

It will print the array in the console so I know the loop is working but PrintWriter wont print anything in the file.

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

Upvotes: 1

Views: 144

Answers (2)

yelliver
yelliver

Reputation: 5936

use flush()

pr.flush();

or create autoflush PrintWriter

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

Upvotes: 1

Maur&#237;cio Linhares
Maur&#237;cio Linhares

Reputation: 40333

Include a:

pr.flush();

Just after your loop.

Upvotes: 1

Related Questions