简体   繁体   English

System.out.println,System.err.println

[英]System.out.println , System.err.println

I have this code: 我有这个代码:

 System.err.print("number of terms =   ");
 System.out.println(allTerms.size());
 System.err.print("number of documents =   ");
 System.out.print("number of documents =   ");

I think the results should be as this: 我认为结果应如下:

number of terms = 10
number of documents =1000

but the results was 但结果是

10
1000
number of terms =   number of documents =

Why, and how to solve it? 为什么,以及如何解决它?

The streams out and err are independent. 流出和错误是独立的。

To get the desired output you must flush the streams or just use just one stream for all outputting. 要获得所需的输出,您必须刷新流或只使用一个流进行所有输出。

To solve it 解决它

System.out.print("number of terms = ");

System.out.println(allTerms.size());

System.out.print("number of documents = ");

System.out.print("number of documents = ");

System.out.println -> Sends the output to a standard output stream. System.out.println - >将输出发送到标准输出流。 Generally monitor. 一般监控。

System.err.println -> Sends the output to a standard error stream. System.err.println - >将输出发送到标准错误流。 Generally monitor. 一般监控。

System.out.print ("He");  
System.out.print ("llo ");  
System.out.println ("World");  

is guaranteed to print "Hello World", but 保证打印“Hello World”,但是

System.out.print ("He");  
System.err.print ("llo ");  
System.out.println ("World");  

might print "llo He World" or "HeWorld llo". 可能会打印“llo He World”或“HeWorld llo”。 They are 2 different streams. 它们是两种不同的流。

System.err.print() prints to stderr which may not show up in the console. System.err.print()打印到stderr,它可能不会出现在控制台中。 Switch them to System.out.print() 将它们切换到System.out.print()

System.err and System.ou are two different things. System.err和System.ou是两回事。 With System.out you are writign to stdout with system.err you are writing to stderr. 使用System.out,您将使用system.err写入stdout,您正在写入stderr。

Try to replace System.err with System.out and you see that your problem disappear. 尝试用System.out替换System.err,您会发现问题消失了。

Then you have to replace: 然后你必须替换:

System.err.print("number of terms = ");

with

System.out.print("number of terms = ");

To change the color of the println check that question: How to color System.out.println output? 要更改println的颜色,请检查该问题: 如何为System.out.println输出颜色?

After every print* statement, use respective stream's flush method. 在每个print *语句之后,使用相应的stream的flush方法。 That may give you desired out put. 这可能会让你想要出局。

System.err.print("number of terms =   "); System.err.flush();  
System.out.println(allTerms.size()); System.out.flush();  
System.err.print("number of documents =   "); System.err.flush();  
System.out.print( numberOfDocuments ); System.out.flush();  

Will result in: 将导致:

number of terms =   10
number of documents =   1000

Hoping you are doing a console app, and as you expected err prints in red. 希望你正在做一个控制台应用程序,并且正如你预期的err打印为红色。

这可能是原因: errout都有一个不同的输出流,它将根据访问控制台的顺序打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM