我使用Process来运行本机二进制文件。这不是问题,我可以正常使用InputStream并通过Scanner解析它。如果我while (scanner.hasNextLine())
和System.out.println(scanner.nextLine())
内,一切都很好。如果我在System.out.println(scanner.nextLine())
内运行runOnUiThread()
,那么它会混淆我的输出,我不会得到所有内容,但我得到的是同一行的20倍(每次运行的数字不一样)。
在这里,我的完整代码:
processScanner = new Scanner(process.getInputStream());
while(processScanner.hasNextLine()) {
line = processScanner.nextLine();
System.out.println(line);
runOnUiThread(new Runnable() {
@Override
public void run() {
cuberiteSTDOUT.writeString(line);
}
});
}
创建正常输出。
processScanner = new Scanner(process.getInputStream());
while(processScanner.hasNextLine()) {
line = processScanner.nextLine();
runOnUiThread(new Runnable() {
@Override
public void run() {
System.out.println(line);
cuberiteSTDOUT.writeString(line);
}
});
}
创建像
这样的疯狂输出02-28 12:44:18.433 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
02-28 12:44:18.435 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
02-28 12:44:18.441 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
02-28 12:44:18.442 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
02-28 12:44:18.443 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
02-28 12:44:18.447 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
02-28 12:44:18.449 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
02-28 12:44:18.450 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
02-28 12:44:18.450 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
02-28 12:44:18.451 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
02-28 12:44:18.452 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
02-28 12:44:18.453 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
02-28 12:44:18.454 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
02-28 12:44:18.456 30908-30908/org.cuberite.android I/System.out: Log: [3d7cb4f2|11:44:18] Created arrow 182 with speed {0.00, 0.00, 0.00} and rot {0.00, 0.00}
(这只是重复的一些行,但其他人遗漏了)
先谢谢
答案 0 :(得分:2)
你的线在另一个线程中被操纵而不是被打印。 将行更改为:
final String line = processScanner.nextLine();