我在这些小代码中与控制台输出混淆:
public static void main(String[] args) throws Exception {
String path = "/hi/my/path";
System.out.println(path);
System.err.println("ERROR!!!");
}
我希望以线性方式输出:
/hi/my/path
ERROR!!!
但我得到了:
我也尝试使用flush()
中的public static void main(String[] args) throws Exception {
String path = "/hi/my/path";
System.out.println(path);
System.out.flush();
System.err.println("ERROR!!!");
System.err.flush();
}
:
public static void main(String[] args) throws Exception {
String path = "/hi/my/path";
System.out.println(path);
System.err.println(path);
System.out.println("ERROR!!!");
System.err.println("ERROR!!!");
}
但是得到了:
如果我跑:
public static void main(String[] args) throws Exception {
String path = "/hi/my/path";
System.out.println(path);
System.out.flush();
System.err.println(path);
System.err.flush();
System.out.println("ERROR");
System.out.flush();
System.err.println("ERROR!!!");
System.err.flush();
}
我明白了:
甚至更奇怪,如果我执行这个:
err
我明白了:
我有什么遗失的东西吗?当我调试它时,一切都以线性方式进行,输出是预期的输出,但是当我正常运行程序时却没有。
相对于out
,System.err
输出流的优先级是否更高?对于某些事情,System.out
比PrintStream
更快吗?
查看possible duplicate源代码,System
和err
均为class DescubreAQue: NSObject {
var frase : String
init( frase :String) {
self.frase = frase
}
class AQueVinimosAEsteMundo: UIViewController {
var frase = ""
var data : [DescubreAQue] = []
var pounter : Int = 0
@IBOutlet weak var labelTexto: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
populate()
}
func populate() {
data.append(DescubreAQue (frase: "1"))
data.append(DescubreAQue (frase: "2"))
data.append(DescubreAQue (frase: "3"))
data.append(DescubreAQue (frase: "4"))
data.append(DescubreAQue (frase: "5"))
}
@IBAction func nextButton(sender: AnyObject) {
labelTexto.text = data[pounter].frase
pounter++
if (pounter == data.count) {
pounter = 0
}
}
@IBAction func previousButton(sender: AnyObject) {
labelTexto.text = data[pounter].frase
pounter-- ;
if ( pounter <= 0) {
pounter = 0
}
}
。
知道为什么会这样吗?