我一直在尝试将数据从命令提示符写入 JTextArea,但它不想为我工作,所以我尝试将数据写入文本文件。到目前为止,它只写了一行然后停止,所以我需要不断地从文本文件中读取,直到停止为止。这是我的代码:`
try {
File consoleLog = new File("tempConsole.txt");
Process p = Runtime.getRuntime().exec("cmd /c minecraft.lnk");
//writes the text from the console to tempConsole.txt
BufferedReader input = new BufferedReader (new InputStreamReader(p.getInputStream()));
BufferedWriter consoleOutputWriter = new BufferedWriter(new FileWriter("tempConsole.txt"));
consoleOutputWriter.write("" + input);
consoleOutputWriter.newLine();
//reads the tempConsole.txt
BufferedReader consoleOutputReader = new BufferedReader (new FileReader("tempConsole.txt"));
//writes the tempConsole.txt to the on-sceen JTextArea.
String outputFromTemp = consoleOutputReader.readLine();
console.setText(outputFromTemp);
consoleOutputWriter.close();
} catch (Exception ex) {`
感谢您的帮助,我已经在我的大脑和互联网上搜索了几个小时但没有运气:/
最佳答案
BufferedReader in = new BufferedReader(new FileReader(fileName))
String line2;
while ((line2 = in.readLine()) != null) {
//do something
}
关于java - 如何从文本文件中连续读取数据 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10853792/