Search code examples
javaswingfilefile-iojtextarea

How to write multiple lines in a file when this multiple lines take as a input from a JTextarea?


I take multiple lines as a input from a JTextarea, If I write it in a file, then I get that the multiple lines are write in a one line in file

Examples:

In JTextArea:

I
am
a
student

Means: variable.text="I'\n'am'\n'a'\n'student"; When I write the string s in file I get:

I am a student

But I want that the file will contain the same things as I give as a input means--->

I
am
a
student

This is the code of writing a file :

      BufferedWriter out = new BufferedWriter(
       new OutputStreamWriter(
                  new FileOutputStream(file), "UTF16"));
      int size=1;
      for(Tableclass variable:tablevector)
      {
            out.write(variable.Text); 
            out.newLine();
            size++;

      }
      out.close();

Solution

  • Slighty better version would be :

        try {
            PrintWriter fstream = new PrintWriter(new FileWriter("log.txt"));
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    
    
     for(String word : jTextAreaName.getText().split("\n"))  {    
        fstream.println(word); 
    } 
         fstream.flush();