java - 如何从文本文件中连续读取数据 - Java

标签 java file jtextarea bufferedreader bufferedwriter

我一直在尝试将数据从命令提示符写入 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/

相关文章:

c++ - 如何检查文件是否存在并且在 C++ 中是否可读?

java - 打印前预览 JTextArea

java - JTextArea 仅包含数字,但允许负值

java - 从 Java 中的字符串数组的索引命名类的实例的问题

java - 使用 Spring 和 Thymeleaf 嵌入具有精确时间的 YouTube 视频

file - 使用 ftp 从远程机器下载文件的 shell 脚本

C++:读取没有扩展名的文件

Java 类让我非常困惑

java - 数组对象和元素?

java - 在将字符串添加到 JTextArea 之前对其进行着色