Assim
Assim

Reputation: 441

JTextArea to file

I want to save text from a JTextArea to a file, the code below works perfectly fine but the only thing is that the line breaks aren't converted. This means no matter how many lines I have in the JTextArea, they are all displayed in one line in the text file.

BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.append(textArea.getText());
writer.close();

What should I do to fix this problem?

Upvotes: 2

Views: 1904

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

A decent solution is to use the Writer that comes with the JTextArea itself. Hang on,... example to come...

Edit Example below:

BufferedWriter writer = new BufferedWriter(new FileWriter(file, true)); // true for append
textArea.write(writer);
writer.close();

Upvotes: 4

Related Questions