4

I have an output file for a program I have written. It is written by a FileWriter and BufferedWriter.

    FileWriter errout = new FileWriter(new File("_ErrorList.txt"));
    BufferedWriter out = new BufferedWriter(errout);

Later I write to the file using lines similar to.

    out.write("Product id:" + idin + " did not fetch any pictures.\n ");

When I simpily run the program in Eclipse, the output file is formatted correctly, with each message being written on a new line. However when I export to a .jar file, it no longer works and puts every message on a single line, as if the "\n" was not working.

Am I using the FileWriter/BufferedWriter incorrectly, or does it not work in a .jar file?

user1816892
  • 85
  • 3
  • 8

2 Answers2

5

You should not use '\n' directly. Either use out.newLine() to introduce a line break, or wrap the BufferedWriter into a PrintWriter, and use out.println().

This has nothing to do with the .jar file, anyway. More likely is Eclipse being clever and showing you line breaks, while the operating system does not.

Flavio
  • 11,925
  • 3
  • 32
  • 36
4

One, check that the line separator is valid. Use System.getProperty("line.separator") as provided by @Andrew Thompson.

Another option if you're doing a lot of this writing new lines is to wrap your BufferedWriter in a PrintWriter.

  FileWriter errout = new FileWriter(new File("_ErrorList.txt"));
  BufferedWriter out = new BufferedWriter(errout);

  PrintWriter printWriter = new PrintWriter(out);
  printWriter.println("Product id:" + idin + " did not fetch any pictures.");
tjg184
  • 4,508
  • 1
  • 27
  • 54