0

My requirement is to generate a text file.I added the new line in my file using bufferedWriter.newLine() its working fine in windows but its not working in linux. I tried too many things but nothing is worked out so for.If i open the generated text file in notepad++ its show as line separated but in notepad it looks with out any line separator.In the console i can find the new line in both the platform but the file only windows has the new line but not in linux.

Below is my code

File file = new File(fileToWrite);
bufferedWrtier = new BufferedWriter(new FileWriter(file));
LOGGER.debug("Line Separator 1:: "+System.getProperty("line.separator"));
LOGGER.debug("Line Separator 2:: "+System.lineSeparator());
LOGGER.debug("Line Separator 3:: "+SystemUtils.LINE_SEPARATOR);
bufferedWrtier.write(System.getProperty("line.separator"));
bufferedWrtier.write(System.lineSeparator());
bufferedWrtier.write(SystemUtils.LINE_SEPARATOR);

Any help will be greatly appreciated!!!!!!!!!!

Local Machine Os: windows Server Machine Os: Linux

Selva
  • 1,620
  • 3
  • 33
  • 63
  • Notepad can not handle the unix EOL which is only a LF. It can only handle the Windows CRLF. So it is no Problem with your program, it is a "problem" with Notepad. – Jens Oct 21 '16 at 12:51
  • @jens, then their is no fix for this?? – Selva Oct 21 '16 at 12:55
  • The Windows Notepad.exe is a very basic application that cannot cope with text files with lines ending with Line Feed (a.k.a. Unix line separators). Your options are: (1) use another tool such as Notepad++; (2) copy the file to your Windows box using a copy utility that translates Unix line endings to Windows line endings. – Klitos Kyriacou Oct 21 '16 at 12:56
  • @KlitosKyriacou, can you please explain the second point in little bit briefly – Selva Oct 21 '16 at 13:00
  • 1
    @Madhesh You can if you use `write("\r\n")` instead of `writeLine()` – Jens Oct 21 '16 at 13:00
  • @jens,I tried that also but no luck – Selva Oct 21 '16 at 13:01
  • @Madhesh But that should work, because this is the Windows EOL – Jens Oct 21 '16 at 13:01
  • @Jens.Its worked.thank u very much. – Selva Oct 21 '16 at 13:35

1 Answers1

1

What you're seeing here is a difference in newline representation, based on the OS you're using.

There are two control characters you need to know:

  • CL - carriage return ('\r' or 0x0D in hex)
  • LF - line feed ('\n' or 0x0A in hex)

Windows natively uses the CL+LF combination for newlines (\r\n combination, or two bytes) while Linux uses only LF (\n, one byte).

Notepad is a pretty limited tool and it can correctly interpret only the Windows combination - if it sees a sequence of two bytes - 0x0D followed by 0x0A in your file, it will display that as a new line.

If it only sees 0x0A without 0x0D, it will not treat it as a new line combination, but will display 0x0A as a box (or it will just be ignored on Windows 10), since it is a non printable character.

To see effects of this, in Notepad++, turn on viewing of all characters:

View -> Show symbol -> Show all characters

You will see that a Windows file has CRLF, while Unix-like has LF.

Unix-like will not be displayed correctly in notepad.

You can switch between Windows and Unix-like like this:

Edit -> EOL conversion

I'd suggest reading the Wikipedia article to learn more about it and a SO post to know more.

Community
  • 1
  • 1
darioo
  • 46,442
  • 10
  • 75
  • 103