Java-反向读取行的输出
下面的例程输出从国际象棋引擎移动到JTextAreaJava-反向读取行的输出,java,line,reverse,Java,Line,Reverse,下面的例程输出从国际象棋引擎移动到JTextArea public void getEngineOutputOriginal(Process engine) { try { BufferedReader reader = new BufferedReader (new InputStreamReader (engine.getInputStream ()), 1); String lineRead = null;
public void getEngineOutputOriginal(Process engine)
{
try {
BufferedReader reader =
new BufferedReader (new InputStreamReader (engine.getInputStream ()), 1);
String lineRead = null;
// send engine analysis to print method
while ((lineRead = reader.readLine ()) != null)
Application.showEngineAnalysis (lineRead);
}
catch (Exception e) {
e.printStackTrace();
}
}
样本输出将是
12 3.49 39/40? 2. b4 (656Knps)
12 3.49 40/40? 2. Nd5 (656Knps)
12-> 3.51 0.04 2. Bxf4 Be6 3. Be3 Qa5 4. Nd5 Qxd2
13 3.51 1/40? 2. Bxf4 (655Knps)
是否可以反转该过程,使最后一行读取始终显示在顶部而不是底部,如:
13 3.51 1/40? 2. Bxf4 (655Knps)
12-> 3.51 0.04 2. Bxf4 Be6 3. Be3 Qa5 4. Nd5 Qxd2
12 3.49 40/40? 2. Nd5 (656Knps)
12 3.49 39/40? 2. b4 (656Knps)
我研究过谷歌,但找不到解决方案当然!一种选择是将行缓冲到
ArrayList
,然后在末尾以相反的顺序显示它们:
List<String> lines = new ArrayList<String>();
/* Add all lines from the file to the buffer. */
while((lineRead = reader.readLine()) != null) {
lines.add(lineRead);
}
/* Replay them in reverse order. */
for (int i = lines.size() - 1; i >= 0; i--) {
Application.showEngineAnalysis(lines.get(i));
}
List line=new ArrayList();
/*将文件中的所有行添加到缓冲区*/
而((lineRead=reader.readLine())!=null){
行。添加(行读取);
}
/*按相反的顺序重放它们*/
对于(int i=lines.size()-1;i>=0;i--){
Application.showEngine分析(lines.get(i));
}
从概念上讲,您可以将其视为制作一个堆栈,将所有行推到堆栈上,然后一次弹出一行
希望这有帮助 听起来不错-谢谢你的快速回复。我会试一试。代码工作得很好,但是我只在停止例程运行时看到反向分析,即它在运行时不会刷新,等等。有什么想法吗?Thanks@user1432365-哦,我明白你的意思了。我认为当您运行上述程序时,该过程已经完成。您可能需要查看您的记录器以了解有关如何反转输出的详细信息。当您说logger时,您是指我的输出方法,即:public static void showEngineAnalysis(String message){if(message.length()>0)message+=“\n”;Application.obj.engineOutput.append(message);}@user1432365-是,您需要更新该方法,使其显示在顶部而不是底部。我不知道Application.obj.engineOutput是什么,但您应该只是预先添加消息,而不是附加消息。