System.out&的随机打印顺序System.err调用 [英] Random printing order for System.out & System.err calls

查看:125
本文介绍了System.out&的随机打印顺序System.err调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



pre $ 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;

尝试{


//我只在windows上运行&因此路径:)
文件文件=新文件(D:\\ Users \\\\\\\\\\\\\\\\\\\\\\\\\\'
fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader); ((str = bufferedReader.readLine())!= null){
System.err.println(str);

$ b $ catch(Exception exception){
System.err.println(读取文件时发生错误:+ exception.getMessage());
exception.printStackTrace();
}
finally {
if(fileReader!= null){
try {
fileReader.close();
System.out.println(最后被执行,文件流被关闭);
catch(IOException ioException){

ioException.printStackTrace();
}
}
}

}

}

当我多次执行代码时,我会像下面那样随机获得输出,有时候System.out语句首先被打印在控制台中,有时System.err会先打印出来。下面是我得到的随机输出

输出1



 文件流被关闭。 
这是一个文本文件
,一个java程序将读取这个文件。



输出2



 这是一个文本文件
,一个java程序将读取这个文件。
最后执行。文件流关闭。

为什么会这样?

解决方案

我相信这是因为你正在写两个不同的输出(一个是标准输出和另一个标准错误)。这些可能在运行时由两个不同的线程处理,以允许在Java执行期间写入两者。假设情况如此,cpu任务调度器不会按照相同的顺序执行线程。

如果所有的输出都转到相同的输出流,那么你永远都不应该得到这个功能(也就是说,所有的东西都是标准输出,或者一切都是标准的错误)。标准错误与标准输出的执行顺序永远无法保证。

Please see the 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 multiple times , I get output randomly as below , sometimes the System.out statement gets printed first in the console , sometimes the System.err gets printed first. below are the random outputs 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 ?

解决方案

I believe this is because you are writing to two different outputs (one is standard out and the other standard error). These are probably handled by two different threads at runtime to allow writing to both during java execution. Assuming this is the case, the cpu task scheduler is not going to execute the threads in the same order every time.

You should never get this functionality if all of your output is going to the same output stream (ie everything goes to standard out or everything goes to standard err). You will never be guaranteed execution order of standard error vs standard output.

这篇关于System.out&的随机打印顺序System.err调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆