gpt4 book ai didi

java - System.out 和 System.err 调用的随机打印顺序

转载 作者:搜寻专家 更新时间:2023-11-01 01:54:54 24 4
gpt4 key购买 nike

<分区>

请看下面的代码片段

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

}

}

当我多次执行代码时,我得到如下随机输出,有时 System.out 语句首先在控制台打印,有时 System.err 首先打印。下面是我得到的随机输出

输出1

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

输出2

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

为什么会这样?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com