Reputation: 311
I create a text file with some headers and then close it, afterwards when I have data that I wish to write to that file nothing happens,my code is below and the stack trace that I get when I try to print to it.
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)
This is hard to understand as I have read that I should always close a Stream after writing to it.
Upvotes: 2
Views: 2384
Reputation: 62906
On this line, you add "//"
to the path:
File new_cal = new File("C:\\Activity_Calibrator\\log\\" + machine + "//" + machine + ".txt");
You probably meant to add "\\"
.
I think this will mess up file creation, but I'm not sure exactly how without testing. It may even be OS dependent. Print file name returned by File.getCanonicalFile()
to be sure.
Upvotes: 1
Reputation: 328913
nothing happens, and I do not get an error
A good first step would be not to swallow exceptions - for example you could print the stack trace to the console:
} catch (Exception e) {
e.printStackTrace();
}
Then you will get a proper exception message which should help you find your issue.
Upvotes: 5