System.err.println()的输出位置;即使代码中的位置相似,输出也会变化。请查看具有相同位置的System.err.println()的4个案例;在它,但它在产出中的位置是变化的。 请在评论中查看程序最后的输出。请解释执行顺序。下面的代码可以自己查看令人惊讶的输出。
public class System_Ex6 {
public static void main(String[] args)throws Exception {
System.out.println("-----------------case 1-------------------");
System.out.println("Java");
System.err.println("ErrorStatement<======Please observe the position of it "); //output comes in red ink color
System.out.println(System.getProperty("java.version"));
System.out.println(System.getProperty("os.name"));
//System.out.println(System.getProperty("os.version"));
System.out.println(System.getProperty("java.Vendor"));
//System.out.println(System.getProperty("os.version"));
System.gc();
System.out.println("-----------------case 2-------------------");
System.out.println("Java");
System.err.println("ErrorStatement<======Please observe the position of it "); //output comes in red ink color
System.out.println(System.getProperty("java.version"));
System.out.println(System.getProperty("os.name"));
//System.out.println(System.getProperty("os.version"));
System.out.println(System.getProperty("java.Vendor"));
//System.out.println(System.getProperty("os.version"));
System.gc();
System.out.println("-----------------case 3-------------------");
System.out.println("Java");
System.err.println("ErrorStatement<======Please observe the position of it "); //output comes in red ink color
//System.out.println(System.getProperty("java.version"));
System.out.println(System.getProperty("os.name"));
System.out.println(System.getProperty("os.version"));
System.out.println(System.getProperty("java.Vendor"));
//System.out.println(System.getProperty("os.version"));
System.gc();
System.out.println("-----------------case 4-------------------");
System.out.println("Java");
System.err.println("ErrorStatement<======Please observe the position of it "); //output comes in red ink color
System.out.println(System.getProperty("java.version"));
System.out.println(System.getProperty("os.name"));
//System.out.println(System.getProperty("os.version"));
System.out.println(System.getProperty("java.Vendor"));
System.out.println(System.getProperty("os.version"));
System.gc();
}//main()
}//class
/*
output:
=======
-----------------case 1-------------------
Java
1.8.0_74
Windows 8
null
ErrorStatement<======Please observe the position of it
-----------------case 2-------------------
Java
1.8.0_74
ErrorStatement<======Please observe the position of it
Windows 8
null
-----------------case 3-------------------
Java
ErrorStatement<======Please observe the position of it
Windows 8
6.2
null
-----------------case 4-------------------
Java
1.8.0_74
Windows 8
null
6.2
ErrorStatement<======Please observe the position of it
*/
答案 0 :(得分:0)
当我在我的系统上运行程序时,ErrorStatement
行的相对位置在所有情况下都是相同的。
这就是我所看到的:
$ java System_Ex6
-----------------case 1-------------------
Java
ErrorStatement<======Please observe the position of it
1.8.0_102
Linux
null
-----------------case 2-------------------
Java
ErrorStatement<======Please observe the position of it
1.8.0_102
Linux
null
-----------------case 3-------------------
Java
ErrorStatement<======Please observe the position of it
Linux
4.7.5-100.fc23.x86_64
null
-----------------case 4-------------------
Java
ErrorStatement<======Please observe the position of it
1.8.0_102
Linux
null
4.7.5-100.fc23.x86_64
$ java -version
openjdk version "1.8.0_102"
OpenJDK Runtime Environment (build 1.8.0_102-b14)
OpenJDK 64-Bit Server VM (build 25.102-b14, mixed mode)
您所看到的是特定于实现的工件。它可能发生在Java或控制台程序中,它正在读取写入System.out / System.err的数据,并将其写入您正在看到的滚动文本缓冲区中......并从中复制粘贴。
底线:未指定此行为,您无法依赖它。不同系统的不同之处并不令人惊讶。
答案 1 :(得分:-1)
System.out
和System.err
是两个不同的流,并在不同时间刷新。