How to use getText() method on a textarea and write to txt file as it appears in textarea?

435 Views Asked by At

Hi I am trying to use the getText() method from a JTextArea and write that into a .txt file. Here is my code:

try {
     String = "OrderDetails.txt";
     PrintWriter output = new PrintWriter(fileName);
     output.println("Customer Details");
     // this part is not important
     output.println(txtAreaOutput.getText()); //Spits out multiple lines into a single line :(
     output.close();
} catch (FileNotFoundException ex) {
     Logger.getLogger(FamousFavoriteSubs.class.getName()).log(Level.SEVERE, null, ex);
}

As you can imagine the textarea has multiple rows text and I want to maintain that when I save the text to the .txt file. Is there a way to do this?

1

There are 1 best solutions below

1
On

The JTextArea has a "write" function to which you have to send a BufferedWriter.

Actually this was answered multiple times, so please google the problem before posting here.

Writing JTextArea content into file