简体   繁体   中英

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

}

Two issues.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM