Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing JTextArea content into file

I have one JTextArea and a Submit button in Java Swing. Need to write the content of textarea into a file with line breaks. I got the output like, it is written as one string in the file.

try {
    BufferedWriter fileOut = new BufferedWriter(new FileWriter("filename.txt")); 
    String myString1 =jTextArea1.getText();
    String myString2 = myString1.replace("\r", "\n");

    System.out.println(myString2);

    fileOut.write(myString2);
    fileOut.close();
} catch (IOException ioe) {
    ioe.printStackTrace();
}

Please get me some suggestions

like image 424
SK0004 Avatar asked Nov 30 '22 06:11

SK0004


1 Answers

Why not use JTextArea's built in write function?

JTextArea area = ...
try (BufferedWriter fileOut = new BufferedWriter(new FileWriter(yourFile))) {
    area.write(fileOut);
}
like image 129
Jeffrey Avatar answered Dec 04 '22 21:12

Jeffrey