"\ n" does not work when exporting to .jar file

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

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

Later I write to a file using lines similar to.

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

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

Am I using FileWriter / BufferedWriter incorrectly or not working in a .jar file?

+4
source share
2 answers

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

This has nothing to do with the .jar file. Most likely, Eclipse is smart and shows line breaks, while the operating system does not.

+5
source

First, make sure the line separator is valid. Use System.getProperty ("line.separator") provided by @Andrew Thompson.

Another option, if you are doing a lot of these new lines, is to wrap the BufferedWriter in 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."); 
+4
source

Source: https://habr.com/ru/post/1446458/


All Articles