Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strings written to file do not preserve line breaks

I am trying to write a String(lengthy but wrapped), which is from JTextArea. When the string printed to console, formatting is same as it was in Text Area, but when I write them to file using BufferedWriter, it is writing that String in single line.

Following snippet can reproduce it:

public class BufferedWriterTest {
    public static void main(String[] args) throws IOException {
        String string = "This is lengthy string that contains many words. So\nI am wrapping it.";
        System.out.println(string);
        File file = new File("C:/Users/User/Desktop/text.txt");
        FileWriter fileWriter = new FileWriter(file);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write(string);
        bufferedWriter.close();
    }
}

What went wrong? How to resolve this? Thanks for any help!

like image 272
Ahamed Avatar asked Feb 08 '12 18:02

Ahamed


People also ask

How do I preserve line breaks when getting text from a textarea?

To preserve line breaks when getting text from a textarea with JavaScript, we can replace whitespace characters with '<br>\n' . const post = document. createElement("p"); post. textContent = postText; post.

How do you keep a line break in HTML?

Preserve Newlines, Line Breaks, and Whitespace in HTML If you want your text to overflow the parent's boundaries, you should use pre as your CSS whitespace property. Using white-space: pre wraps still preserves newlines and spaces. Enjoy!

How do you line break a string?

Inserting a newline code \n , \r\n into a string will result in a line break at that location. On Unix, including Mac, \n (LF) is often used, and on Windows, \r\n (CR + LF) is often used as a newline code.

How do you stop a line break in Java?

String text = readFileAsString("textfile. txt"); text. replace("\n", "");


Video Answer


2 Answers

Text from a JTextArea will have \n characters for newlines, regardless of the platform it is running on. You will want to replace those characters with the platform-specific newline as you write it to the file (for Windows, this is \r\n, as others have mentioned).

I think the best way to do that is to wrap the text into a BufferedReader, which can be used to iterate over the lines, and then use a PrintWriter to write each line out to a file using the platform-specific newline. There is a shorter solution involving string.replace(...) (see comment by Unbeli), but it is slower and requires more memory.

Here is my solution - now made even simpler thanks to new features in Java 8:

public static void main(String[] args) throws IOException {
    String string = "This is lengthy string that contains many words. So\nI am wrapping it.";
    System.out.println(string);
    File file = new File("C:/Users/User/Desktop/text.txt");

    writeToFile(string, file);
}

private static void writeToFile(String string, File file) throws IOException {
    try (
        BufferedReader reader = new BufferedReader(new StringReader(string));
        PrintWriter writer = new PrintWriter(new FileWriter(file));
    ) {
        reader.lines().forEach(line -> writer.println(line));
    }
}
like image 66
Kevin K Avatar answered Oct 06 '22 00:10

Kevin K


Please see the following question on how to appropriately handle newlines.

How do I get a platform-dependent new line character?

Basically you want to use

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

and then use the newLineChar instead of "\n"

like image 28
jluzwick Avatar answered Oct 06 '22 01:10

jluzwick