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

10,991

Solution 1

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();

Solution 2

Use out.newLine();

   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 3

find this char in your string char(10) or char(13)

int index = textarea.firstIndexOf(CHAR(10));
Share:
10,991
Animesh Kumar Paul
Author by

Animesh Kumar Paul

Updated on August 21, 2022

Comments

  • Animesh Kumar Paul
    Animesh Kumar Paul over 1 year

    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();