2

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.

DavyGravy
  • 311
  • 1
  • 4
  • 14

2 Answers2

5

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.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • Never swallow any exceptions without at least a comment on why it is ignored. Never catch Exception, except for logging and re-throwing it. Never ever use `try {` ... `} catch (Exception e) {/*empty*/};` in any code, even for debugging, because you'll forget it there one time and destroy the world. – hyde Oct 31 '12 at 11:39
  • Ok I have done that and can see that it tells me that the Stream is closed, but everywhere I read about this I am told that one should close the Stream everytime it is written to. – DavyGravy Oct 31 '12 at 11:42
  • What method call throws the exception? Also, check the file name, is it what you think it is? – hyde Oct 31 '12 at 11:45
  • @hyde I think it is fine to use it on a temporary basis while debugging (or not catch it at all and let it propagate up to main) - but I agree it is not a good fit for production code - however it does not sound like the OP is going is going to release that code to a wide audience soon. – assylias Oct 31 '12 at 11:46
  • @DavidDrennan Once you have closed it you can't write to it any longer. So you can either keep it open while you are still using it - or if that would lead you to keep it open for a long period, reopen it when required. See for example: http://stackoverflow.com/questions/9782706/opening-an-existing-file-in-java-and-closing-it – assylias Oct 31 '12 at 11:48
  • @assylias Yeah, but even for debugging, catching Exception is *usually* doing it wrong. After an exception, the program's internal state may be inconsistent, and any further debugging is pointless before either handling the exception properly, or making it so it does not get thrown. – hyde Oct 31 '12 at 11:49
  • @assylias Thanks I have done that and now it writes, well it overwrites is there anyway to append the contents of the new writer to the contents already in the file? – DavyGravy Oct 31 '12 at 12:07
  • @assylias ignore the last question I can set that in Filewriter thanks for help – DavyGravy Oct 31 '12 at 12:20
1

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.

hyde
  • 60,639
  • 21
  • 115
  • 176