Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with writing texts in new line  RSS feed

 
Jahangir Alam
Greenhorn
Posts: 15
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to write some texts into a student.txt file. I have written a program and it works fine. But all the information are being written into a single line. I want each in separate line.

Here is the part of code:



I added "\n", but it did not work.

Any help would be great for me!
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One solution would be to wrap your FileWriter object in a BufferedWriter object and then use a newLine() method after every write().
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another option is a Formatter which can write all those details in to a text file. You can pass the File object to its constructor, or a stream leading to that file. You may have to handle a FileNotFoundException.

Why are you parsing a String to an int and immediately converting it back to a String with the + operator? Why not simply use trim() and add the Strings with +.

 
Doug Slavens
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try replacing FileWriter with PrintWriter. PrintWriter has println methods which will print the output of each call on a new line.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!