4

This code:

PrintWriter output = new PrintWriter(new FileWriter(outputFile, false));
output.println("something\n");
output.println("something else\n");

Outputs:

something
something else

Instead of:

something

something else

I tried using "\r\n" instead of just "\n" but it just doesn't work like how I want it to. How do I fix this?

P.S. I'm using windows 7

James P.
  • 19,313
  • 27
  • 97
  • 155
Haque1
  • 575
  • 1
  • 11
  • 21
  • use -- output.print("something\n\n"); – Rahul Agrawal Oct 09 '12 at 08:12
  • 3
    can you post an hexdump of the output file ? – BigMike Oct 09 '12 at 08:12
  • What does output.println("something"); - without any \n - print? – Zarkonnen Oct 09 '12 at 08:13
  • 3
    Are you checking the content of the file with Notepad? It does not display the single `\n` as a line break but it exists as you can verify when you display it with a different texteditor or even hexeditor. – halex Oct 09 '12 at 08:16
  • 2
    Have a look at [this](http://stackoverflow.com/questions/1014287/is-there-a-way-to-make-printwriter-output-to-unix-format) and also check output using an advanced text editor so you can see what characters are actually being output. – James P. Oct 09 '12 at 08:16
  • @JamesPoulson make your comment an answer and you'll get my +1. – BigMike Oct 09 '12 at 08:33
  • @halex and James, Thanks for the tip! I installed notepad++ and things are showing up fine just as expected. – Haque1 Oct 09 '12 at 08:55

5 Answers5

6

You can concatenate system's newline to separate your lines:

    String newLine = System.getProperty("line.separator");
    output.println("something" + newLine);
    output.println("something else" + newLine);
Juvanis
  • 25,802
  • 5
  • 69
  • 87
  • This is also only guaranteed to work if the file is viewed in the same system as the program was run. But it's cleaner than using `\n` or `\r\n` or so. +1 – brimborium Oct 09 '12 at 08:20
2

Your code works like a charm, just check the file with a proper programmers editor. (or as I suggested before, take a look at an hex dump of the file)

BigMike
  • 6,683
  • 1
  • 23
  • 24
2

This

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class Main {
    public static void main(String[] args) {
        PrintWriter output;
        try {
            output = new PrintWriter(new FileWriter("asdf.txt", false));
            output.println("something\n");
            output.println("something else\n");
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Works well for me, I get an asdf.txt like this

something

something else

I am using jre1.7, what are you using?

brimborium
  • 9,362
  • 9
  • 48
  • 76
  • The problem was not java. As advised above, I downloaded an advanced editor (notepad++) and things showed up just fine. Thanks for responding to my post! – Haque1 Oct 09 '12 at 08:56
  • Oõ What editor where you using that was not able to show `\n` properly? – brimborium Oct 09 '12 at 09:50
1

This works perfectly fine. You must be using notepad for the output. Try using a different text editor like notepad++. You'll get your desired output.

pratZ
  • 3,078
  • 2
  • 20
  • 29
0

Try this:

package com.stackoverflow.works;

import java.io.FileWriter;
import java.io.PrintWriter;

/*
 * @author: sarath_sivan
 */
public class PrintWriterExample {

    private static final String NEW_LINE = System.getProperty("line.separator");

    public static void main(String[] args) {
        String outputFile = "C:/Users/sarath_sivan/Desktop/out.txt";
        PrintWriter output = null;
        try {
            output = new PrintWriter(new FileWriter(outputFile, false));
            output.println("something" + NEW_LINE);
            output.println("something else" + NEW_LINE);
            output.flush();
        } catch(Exception exception) {
            exception.printStackTrace();
        } finally {
            if (output != null) {
                output.close();
            }
        }
    }
}

OUTPUT:

Output

1218985
  • 7,531
  • 2
  • 25
  • 31