1

If I run this simple program on Windows 7 and then on AIX (Unix system) and compare the two generated files using a tool such as Winmerge or Compare It, it tells me that the Carriage Return and Line Feed are different but the content identical.

  • Why is that? Isn't supposed to be the same if both use the same encoding "UTF-8" in this case?

  • How can I make both files totally equal?

public class WriteFile {

    public static void main(String[] args) throws IOException {
        write();

    }

    public static void write() throws IOException {
        File file = new File("/tmp/test.txt");
        file.delete();
        file.createNewFile();
        String str = "hello";
        FileOutputStream fileOs = new FileOutputStream(file);
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(fileOs, "UTF-8"), true);
        writer.println(str);
        writer.close();
    }

}
Dawnkeeper
  • 2,844
  • 1
  • 25
  • 41
Alfredo Osorio
  • 11,297
  • 12
  • 56
  • 84

3 Answers3

5

Different operating systems use different newline conventions:

  • On Windows, newlines are CR+LF;
  • On Unix, newlines are LF.

(If you're curious, there's more.).

If you need the output files to be identical, don't use println(), but write \n or \r\n instead:

writer.printf("%s\n", str);   // LF
writer.printf("%s\r\n", str); // CR+LF
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Thank you. It worked. So calling writer.println is platform dependent. Also if I want to use CR+LF I can also use writer.printf("%s\r\n", str). – Alfredo Osorio Oct 13 '11 at 16:01
  • `println` is platform-dependent, yes, *by design*. The idea is it will always work correctly for the platform you're on. But if you need identical outputs from different platforms, then you have to pick which rule you want to follow. – Dan Oct 13 '11 at 16:11
  • @AlfredoO: Yes, `\r\n` will give CR+LF. – NPE Oct 13 '11 at 16:13
3

Use

writer.print(str + "\n");

instead of

writer.println(str);
stivlo
  • 83,644
  • 31
  • 142
  • 199
1

Like it's been said, you should switch from using println(String) to print(String) or printf(String, ...)

Check out the documentation for println(String) (emphasis mine):

Prints a String and then terminates the line. This method behaves as though it invokes print(String) and then println().

And the docs for println():

Terminates the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n').

You can read about the line.separator system property here.

I would go with aix's suggestion of using

writer.printf("%s\n", str);
Zecc
  • 4,220
  • 19
  • 17