Zufällige Druckreihenfolge für System.out- und System.err-Aufrufe
Bitte beachten Sie den folgenden Codeausschnitt
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();
}
}
}
}
}
Wenn ich den Code mehrmals ausführe, werde ich wie folgt zufällig ausgegeben. Manchmal wird die Anweisung System.out zuerst in der Konsole gedruckt, manchmal wird die Anweisung System.err zuerst gedruckt. unten sind die zufälligen Ausgaben, die ich bekomme
Ausgang 1Finally is executed.File stream is closed.
this is a text file
and a java program will read this file.
Ausgang 2this is a text file
and a java program will read this file.
Finally is executed.File stream is closed.
Warum ist das so ?