BufferedWriter 没有写入文件
BufferedWriter isn't writing to file
我正在 Java 中编写程序以读取 Linux 机器上的 CPU 临时数据,并每 4 秒将结果输出到一个文件中。 temps 会按时间间隔成功返回到控制台,但不会出现在文件中,也不会抛出任何错误。这也是我第一次使用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);
}
}
您的编写器正在将每一行写入文件。
但是,由于您重新打开并运行在每一行之前对文件进行分类,并且 /usr/bin/sensors
输出以空行结尾,因此文件将仅包含最后一个空行结束。
查看此内容的最简单方法是告诉您的 FileWriter 追加而不是 t运行cate:
BufferedWriter wr = new BufferedWriter(new FileWriter(file, true));
如果你希望文件包含命令的所有输出,但前提是它与最后一个不同运行,那么显然你不能逐行做出决定.相反,您必须将所有行读入一个字符串,并将其与之前的 运行.
进行比较
我正在 Java 中编写程序以读取 Linux 机器上的 CPU 临时数据,并每 4 秒将结果输出到一个文件中。 temps 会按时间间隔成功返回到控制台,但不会出现在文件中,也不会抛出任何错误。这也是我第一次使用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);
}
}
您的编写器正在将每一行写入文件。
但是,由于您重新打开并运行在每一行之前对文件进行分类,并且 /usr/bin/sensors
输出以空行结尾,因此文件将仅包含最后一个空行结束。
查看此内容的最简单方法是告诉您的 FileWriter 追加而不是 t运行cate:
BufferedWriter wr = new BufferedWriter(new FileWriter(file, true));
如果你希望文件包含命令的所有输出,但前提是它与最后一个不同运行,那么显然你不能逐行做出决定.相反,您必须将所有行读入一个字符串,并将其与之前的 运行.
进行比较