2012-06-01 50 views
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) {` 

谢谢您的帮助,我已经冲刷我的大脑并没有运气小时上网:/

+1

您应该使用的readLine开始()你的BufferedReader的方法来获得输入。现在这一行consoleOutputWriter.write(“”+ input);使用“toString()”,这当然不会起作用。 – Jochen

回答

2
BufferedReader in = new BufferedReader(new FileReader(fileName)) 


String line2; 
while ((line2 = in.readLine()) != null) { 
//do something 
}