I'm making a simple notepad, which will save the content from JTextArea to a File. But i have a problem, i'm not able to save a multiline text.
Here's my code:
JTextArea textArea = new JTextArea();
File writeFile;
FileWriter fileWriter = null;
BufferedWriter bufWriter = null;
writeFile = new File("note.txt");
try {
fileWriter = new FileWriter(writeFile);
bufWriter = new BufferedWriter(fileWriter);
bufWriter.write(textArea.getText());
bufWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
For example, i have a button and a textarea. When I input something like this:
test line 1
test line 2
and press the button to save, the file created. but the contents of the file, become like this
test line 1test line 2
Please give me a detail answer, so i can understand properly. i'm new in java GUI.
Thank you very much.
after trying your code, i see that your code only save the text in a single line. for example in the text area it's looked like this
first line
second line
third line
but in the file you just got
first linesecond linethird line
if that is the case you can use this code
File writeFile;
Writer writer = null;
writeFile = new File("D:\\note.txt");
try {
writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(writeFile), "utf-8"));
jTextArea1.write(writer);
} catch (IOException ex) {
// report
} finally {
try {
writer.close();
} catch (Exception ex) {/*ignore*/
}
}
in this code we use writer from jtextarea itself, so it will save the text as we see at the jtextarea.
hope this help