2

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?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Assim
  • 441
  • 2
  • 6
  • 18

1 Answers1

4

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();
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373