我创建了一个带有一些标题的文本文件,然后将其关闭,然后当我有想要写入该文件的数据时,什么也没有发生,我的代码在下面以及我尝试打印到它时得到的堆栈跟踪。
public class Writetofile {
BufferedWriter writer = null;
public void recordData(String record) throws IOException {
try {
writer.append(record);
writer.newLine();
writer.close();
} catch (Exception e) {e.printStackTrace();
}
}
public void createtxtfile(String[] details) throws IOException {
String machine = details[0];
String date = details[1];
String start_time = details[2];
try {
File new_cal = new File("C:\\Activity_Calibrator\\log\\" + machine + "//" + machine + ".txt");
new_cal.getParentFile().mkdirs();
FileWriter fwriter = new FileWriter(new_cal);
writer = new BufferedWriter(fwriter);
writer.append("Linear Calibration for " + machine + " carried out " + date);
writer.newLine();
writer.close();
} catch (Exception e) {
}
}
}
Message that is recieved
java.io.IOException: Stream closed
at java.io.BufferedWriter.ensureOpen(BufferedWriter.java:116)
at java.io.BufferedWriter.write(BufferedWriter.java:221)
at java.io.Writer.write(Writer.java:157)
at java.io.Writer.append(Writer.java:227)
at Writetofile.recordData(Writetofile.java:27)
at UserInterFace.update(UserInterFace.java:75)
at Comms.serialEvent(Comms.java:124)
at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
at gnu.io.RXTXPort.eventLoop(Native Method)
at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)
这很难理解,因为我已经读过我应该在写入 Stream 后始终关闭它。