Random printing for calls to System.out & System.err

See code snippet below

import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class ReadFile { public static void main(String[] args) { String str=""; FileReader fileReader=null; try{ // I am running on windows only & hence the path :) File file=new File("D:\\Users\\jenco\\Desktop\\readme.txt"); fileReader=new FileReader(file); BufferedReader bufferedReader=new BufferedReader(fileReader); while((str=bufferedReader.readLine())!=null){ System.err.println(str); } }catch(Exception exception){ System.err.println("Error occured while reading the file : " + exception.getMessage()); exception.printStackTrace(); } finally { if (fileReader != null) { try { fileReader.close(); System.out.println("Finally is executed.File stream is closed."); } catch (IOException ioException) { ioException.printStackTrace(); } } } } } 

When I execute the code several times, I get the output randomly, as shown below, sometimes the System.out statement is first printed to the console, sometimes System.err is printed first. below is the random output that I get

Output 1

 Finally is executed.File stream is closed. this is a text file and a java program will read this file. 

Output 2

 this is a text file and a java program will read this file. Finally is executed.File stream is closed. 

Why is this so?

+4
java file-io
source share
3 answers

I believe this is because you are writing to two different outputs (one of them is standard and the other is standard error). They are probably handled by two different threads at runtime, which allows you to write like Java runtime. Assuming this is the case, the cpu task scheduler will not execute threads in the same order every time.

You should never get this functionality if all your output goes to the same output stream (i.e. everything goes to the standard version or everything goes to the standard error). You will never be guaranteed the execution order of standard error against standard output.

+5
source share

Because System.out and System.err both point to the console in your case.

To demonstrate, if you add System.out.flush () and System.err.flush () after println (), the result will be consistent.

+1
source share

The answer has already been given here:

Java: System.out.println and System.err.println do not work

This is because your last sentence uses System.out, and the other code uses System.err. The error stream merges before the output stream or vice versa.

As a result, the print data order will not be guaranteed to be issued in the same order as it is.

You can always connect the console to the error stream to a file or the output stream to a file for later verification. Or change your code to print everything on System.out. Many programmers do not use ERR and the utility is controversial if you do not capture the err separately from the console.

Just use it!

Again and again...

0
source share

All Articles