sohan
sohan

Reputation: 597

flush() method of printwriter doesnt work

I am trying to write to a file, which another process can read. I am using the Printwiter to write to a file. But it doesnt write to the file as long as i dont terminate the program. I have eanbles the autflush on, and even explicitly flusing. The code is below -

import java.io.FileWriter;
import java.io.PrintWriter;

public class ReadFile {

    public static void main(String[] args) {


         try {

              // Create a print writer
             FileWriter fw = new FileWriter("D:\\SpringProjects\\RescilienceModel\\natural_resource.txt");
              //BufferedWriter bw = new BufferedWriter(fw);
              PrintWriter pw = new PrintWriter(fw, true);

              // Experiment with some methods
              while(true)
              {
              pw.println(99);
              pw.flush();
              }
            } catch (Exception e) {
              System.out.println("Exception: " + e);
            }
    }

}

Upvotes: 0

Views: 958

Answers (1)

ergonaut
ergonaut

Reputation: 7057

Two issues.

  1. Check that while(true) loop, or it won't end.

  2. close() your handle, or it won't release resources.

Upvotes: 3

Related Questions