Using "\n " line break in Java

What problem can a Java programmer have when using "\n" to skip line?

Note: I have used it for N reasons, write a file TXT and others.

 11
Author: BOT K3nny, 2014-02-26

5 answers

The only problem is that if your file is opened on Windows (or Mac), some text editors will not recognize the line break and show everything as if it were a single line. If you want to ensure that line breaks are in the format of your current system, it is best to use the system property "line.separator":

String quebraLinha = System.getProperty("line.separator"); // "\n" no Linux, 
                                                           // "\r\n" no Windows
                                                           // "\r" em algumas versões do Mac
 13
Author: mgibsonbr, 2014-02-26 18:10:40

Not every platform uses "\n" as a row separator. Example:

Linux: "\n"
Windows: "\r\n"
Some Macs: "\r"

So one way to know which row separator of the current platform is as follows:

System.getProperty("line.separator"); // Anterior ao Java 7
System.lineSeparator();               // A partir do Java 7

Source: https://stackoverflow.com/questions/14383580/java-end-of-line-with-system-out-print

 15
Author: Guilherme Bernal, 2017-05-23 12:37:23

This question already has some very interesting answers, but I understand that the question is not about how to use the line break but about the possible problems .

Well, I'll name two that I've been through:

  • Linux server using System.getProperty("line.separator") generates text file with \n, but the client opens in Notepad and complains that the file is "unformatted".

  • When importing / exporting text files from / to other systems you must set a OS independent standard to avoid incompatibilities.

Lesson: in web systems, not we can rely on the system property.

The solution in most of these cases is to leave the break hard-coded in the code.

Personally, I prefer to leave \r\n because it works on Windows, system that most users use. Linux and Mac users often use more advanced editors that would recognize line breaks different from OS.

 4
Author: utluiz, 2014-02-26 18:51:27

As others have said, not all systems interpret End Of Line equally. For example, once my hosts File On Windows had, one way or another, if saved with \n and not \r\n. What happened was that Windows failed to bore the file and caused no file entry to be recognized (by ping, etc.)

 3
Author: brazilianldsjaguar, 2014-02-26 18:19:21

In the case of PHP, we have a constant PHP_EOL which means line break, because for some systems the right is to break lines with \r\n and in other systems it is simply \n.

In the case of Perl, there is the method say which is nothing more than a echo with a line break at the end. I believe that the line break character in this case also varies from system to system (although Perl is a language made to work mainly on systems UNIX).

Probably Java has some character or workaround so you don't have to directly use \n or \r\n.

 2
Author: Rodrigo Rigotti, 2014-02-26 18:34:32