0
我一直在嘗試從命令提示符寫入數據到JTextArea,但它不想爲我工作,所以我嘗試將數據寫入文本文件。到目前爲止,它寫了一行然後停止,所以我需要不斷從文本文件中讀取,直到我停止它。這裏是我的代碼:`如何從文本文件連續讀取數據 - Java
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) {`
謝謝您的幫助,我已經沖刷我的大腦並沒有運氣小時上網:/
您應該使用的readLine開始()你的BufferedReader的方法來獲得輸入。現在這一行consoleOutputWriter.write(「」+ input);使用「toString()」,這當然不會起作用。 – Jochen