开发者社区> 问答> 正文

BufferedWriter未写入文件

我正在用Java编写程序,以读取Linux机器上的CPU温度并将结果每4秒输出到一个文件中。这些时间间隔会成功返回到控制台,但不会出现在文件中,也不会引发任何错误。这也是我第一次使用BufferedWriter,因此如果使用不正确,我深表歉意。

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class tempapp{
    public static void main(String[] args) throws IOException, InterruptedException {
        String fileName = "temps.txt";
        TimerTask task = new TimerTask() {
            @Override
            public void run(){
                // task goes here

                List<String> commands = new ArrayList<>();
                //build command
                commands.add("/usr/bin/sensors");
                //args
                //commands.add("");
                System.out.println(commands);

                ProcessBuilder pb = new ProcessBuilder(commands);
                pb.directory(new File("/home/ethano"));
                pb.redirectErrorStream(true);
                Process process = null;
                try {
                    process = pb.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                //Read output
                StringBuilder out = new StringBuilder();
                BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line = null, previous = null;
                while (true) {
                    try {
                        if (!((line = br.readLine()) != null)) break;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if (!line.equals(previous)) {
                        previous = line;
                        out.append(line).append('\n');
                        System.out.println(line);
                        try {
                            File file = new File ("/home/ethano/Desktop/temps.txt");
                            BufferedWriter wr = new BufferedWriter(new FileWriter(file));
                        wr.write(line);
                        wr.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

                //Check result
                try {
                    if (process.waitFor() == 0) {
                        //System.exit(0);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                //weird termination
                //System.err.println(commands);
                //System.err.println(out.toString());
                //System.exit(1);
            }
        };

        Timer timer = new Timer();
        long delay = 0;
        long intervalPeriod = 4 * 1000;

        //schedules task to run in interval
        timer.scheduleAtFixedRate(task, delay, intervalPeriod);


    }
}

问题来源:Stack Overflow

展开
收起
montos 2020-03-27 22:47:00 547 0
1 条回答
写回答
取消 提交回答
  • 您的作家正在将每一行写入文件。

    但是,由于您在每一行之前重新打开并截断了文件,并且/usr/bin/sensors输出以空白行结尾,因此文件将仅在末尾包含最后一个空白行。

    看到这种情况的最简单方法是告诉FileWriter追加而不是截断:

    BufferedWriter wr = new BufferedWriter(new FileWriter(file, true));
    

    如果您希望文件包含命令的所有输出,但前提是该文件与上次运行不同,则显然您不能逐行进行此确定。相反,您必须将所有行读入字符串,然后将其与上一次运行进行比较。

    回答来源:Stack Overflow

    2020-03-27 22:47:23
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载