Dobz
Dobz

Reputation: 1213

Java File I/O String All Stuck Together No New Lines

I have a variable filelist.

filelist = "Example 1\n Example 2\n"

When I do System.out.println(filename) I get:

"Example 1

Example 2"

but when I write to a txt file I get:

"Example 1 Example 2"

How do I make these start on a newline at \n ?

try {
        TextFile = new Formatter("C:\\Text.txt");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        TextFile.format("%s", filename);
        TextFile.close();
    }

Upvotes: 0

Views: 151

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1499760

My guess is that you're running on Windows, which uses \r\n as a line separator by default. (As in, that's what programs such as Notepad interpret as a line separator.)

You could hard-code that, or fetch the platform-specific one from the system properties:

String lineSeparator = System.getProperty("line.separator");

Or use BufferedWriter (or similar) which provide methods to write a new line - again, using the platform-specific default line separator.

Upvotes: 1

Related Questions