Using line break " n" in Java

Asked

Viewed 61,683 times

11

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

Observing: I’ve used it for N reasons, record a file TXT and others.

  • Did you mean "\n"? Moreover the question is not at all objective in the current format. It would be close as wide as too.

  • 1

    Well, maybe it seems to be a question without objective, but with Java is multiplatform, I want to know without in other systems it would cause problem.

  • 1

    Friend I think you should simplify your name to Eduardobrj

  • Thanks for the tip friend, I will do it on 19/03, if you have more suggestions send me.

5 answers

16

13


The only problem is that if your file opens 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 system property "line.separator":

String quebraLinha = System.getProperty("line.separator"); // "\n" no Linux, 
                                                           // "\r\n" no Windows
                                                           // "\r" em algumas versões do Mac
  • It would be advantageous so always use \r\n, for a file to present itself in the same way in all three systems?

  • @Renan As far as I know, there is no "side effect" on having \r remaining on Linux or \n remaining on the Mac, but I think it depends on the application (e.g., specific text editor, program that will process the text, etc.). I imagine if the text is for human consumption, use \r\n would be ideal - as it would ensure that the vast majority of systems recognize line breaks and "left over" nothing (many programs are robust enough to handle two or more line break variations in the input).

4

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 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 "deformed".

  • Importing/exporting text files from/to other systems must set a standalone OS standard to avoid incompatibilities.

Lesson: on web systems, nay we can rely on system property.

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

Personally, I’d rather leave \r\n because it works on Windows, system that most of the users use. Linux and Mac users generally use more advanced editors that would recognize different line breaks from the OS.

3

As others have said, it is not all systems that interpret the End Of Line also. For example, once my file hosts in Windows had, one way or another, if saved with \n and not \r\n. What happened was that Windows failed to understand the file and made no file entry recognized (by ping, etc..)

2

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

In the case of Perl, there is the method say that 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 UNIX systems).

Probably Java has some character or workaround not to have to use directly \n or \r\n.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.