4

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(); 
4
  • 1
    Have you tried adding an out.write("\n") after each variable's text is written? Commented May 10, 2012 at 13:50
  • Do as Vulcan suggests: out.write(variable.Text+"\n"); Commented May 10, 2012 at 13:53
  • I think I can not tell you clearly. Please see the changes. I want say I take a input string like as variable.text="I'\n'am'\n'a'\n'student"; and now I want to write this in file as I am a student Commented May 10, 2012 at 14:17
  • How do you view the file, and determine that there are no newlines? In for example Notepad on Windows, just \n is not enough to cause a linebreak, you need \r\n. When in doubt, inspect the file with a hex editor. Commented Sep 27, 2012 at 8:50

3 Answers 3

3

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(); 
Sign up to request clarification or add additional context in comments.

1 Comment

Ahh thank god, had been stuck for hours and finally !
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(); 

Comments

1

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

int index = textarea.firstIndexOf(CHAR(10)); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.