BufferedWriter isn't writing to file

Ethan Onweller :

I am writing a program in Java to read CPU temps on a Linux machine and output results to a file every 4 seconds. The temps are successfully returned to the console at the interval, but do not appear in the file and no errors are thrown. This is also my first time using BufferedWriter, so I apologize if I am using it incorrectly.

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);


    }
}
that other guy :

Your writer is writing each line to the file.

However, since you re-open and truncate the file before each line, and /usr/bin/sensors output ends in a blank line, the file will only contain that last blank line at the end.

The easiest way to see this is to tell your FileWriter to append instead of truncate:

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

If you want the file to contain all the output of the command, but only if it's different from the last run, then obviously you can't make this determination on a line-by-line basis. You instead have to read all the lines into a String, and compare that to the previous run.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=238593&siteId=1