0

This question already has an answer here:

I tried to output to a text file using redirection operators > and >> from a java class but it ignores line feed \n from java when the .txt file is opened with the Notepad from windows 10. It's interesting because on the prompt the line feed works just fine and if I use System.out.println("") it works normally in the Notepad.

Class with \n line feed:

class n{

    public static void main(String[] args){

        System.out.print("This\n");
        System.out.print("Should\n");
        System.out.print("Be\n");
        System.out.print("Separated.\n");


    }

}

Class using System.out.println(""):

class ln{

    public static void main(String[] args){

        System.out.println("This");
        System.out.println("Should");
        System.out.println("Be");
        System.out.println("Separated.");

    }

}

Output in the prompt:

Prompt

Output in text file from class n (n.txt) opened with Notepad:

ThisShouldBeSeparated.

Output in text file from class ln (ln.txt) opened with Notepad:

This
Should
Be
Separated.

These two above should be equal.

I tested with the class n public but it didn't change the result.

I've searched and found about how to echo a line feed, that's not what I want, I want the redirected text file working like the prompt.

One interesting detail is when I paste the text from n.txt in here (writing a question), in the Microsoft Word, Notepad++ or WordPad (or open the file in the programs cited) it "fixed" the error and was equal the result from class ln.

EDIT

I think it's not a duplicate because when I wrote the question I didn't even think about the possible Notepad problem. The title is completely different so I answered my own question for people who didn't think that the problem is the Notepad, like me before, may get an good solution.

If I'm wrong I would be happy to be corrected.

marked as duplicate by Ƭᴇcʜιᴇ007 windows May 20 '16 at 17:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I posted the Q&A even though I discovered the solution while writing the question because at first I didn't even wrote that was using Notepad because I was sure that the problem was in the prompt. So I think others can have the same problems and dificulty to find the solution to this. – Lucas Souza May 20 '16 at 17:30
  • Questions about Java programming problems are off-topic for SU, and the problem of "my text file doesn't show line breaks in Notepad" has already been covered (as you've pointed out). Voting to close. – Ƭᴇcʜιᴇ007 May 20 '16 at 17:53
  • @Ƭᴇcʜιᴇ007 I maintain the Q&A because I wasn't thinking about the possibly problem in the Notepad and I think other people could think the problem is in the prompt and not in the Notepad like myself before. If this isn't enough to differentiate this question to the "Notepad ignoring linebreaks" than I will be glad to delete it. – Lucas Souza May 20 '16 at 18:56
  • 1
    You're right, duplicate questions with sufficient difference in phrasing can be helpful in pointing people to the right place. Therefore, this question should be preserved, but left marked as duplicate. – Ben N May 20 '16 at 22:56
0

While searching more about line feed and text files I found a question here in the superuser community that explained what is happening.

The problem is not de prompt, is the Notepad from Windows (in my case Windows 10, in the case of the author of the other question Windows 7) that don't recoginize this type of line feed, quoting @Paul who answered the question I'm talking about:

Notepad only recognises CR, LF (0x0d, 0x0a), whereas other sources might use CR only, or LF only.

Now I recommend using as first text editor the WordPad or NotePad++ that work normally.

In the Notepad:

Notepad

In Notepad++ and WordPad:

NotePad++ WordPad

Other possible solution is to copy the text from the Notepad, pasting in the Notepad++, WordPad, Microsoft Word or any text editor that recognize this type of line break, and than copying it back.

For more information go to the question "Notepad ignoring linebreaks".

  • Actually, the problem is that you're generating wrong line breaks. You program outputs Unix line breaks (only one "Line Feed"/LF character, ASCII code 10, "\n" in Java) while DOS and Windows use CR/LF (two characters, one "Carriage Return", ASCII code 13, escape sequence "\r" and one LF character). Refer to the following question to learn how to use the correct newline characters for your operating system: stackoverflow.com/questions/207947/… – Gunter Ohrner Sep 14 '18 at 13:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.