Q:

How to ensure prompt output to the file with BufferedWriter?

I am writing to a text file in a while loop:
BufferedWriter writer = new BufferedWriter("...");
BufferedReader reader = new BufferedReader("...");

while((line = reader.readline()) != null){
    ....
    writer.write(line + "\n");
}

reader.close();
writer.close();

This code doesn't write the line to the file as soon as each line is processed. I want the line to be written into the file in each iteration. How to achieve that? 

A:

writer(line + "\n"); is not a method that exists. writer is the name of the BufferedWriter object. You need to add the method call to print your output with writer.println("...");
You should surround your code in a try-catch clause when working with IO to prevent IOExceptions
A simpler way to do this would be with a PrintWriter instead of a BufferedWriter.

