File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Problem with writing texts in new line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with writing texts in new line" Watch "Problem with writing texts in new line" New topic

Problem with writing texts in new line

Jahangir Alam

Joined: Aug 18, 2013
Posts: 15


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!

Sorry for my bad English!
Ahsan Bagwan
Ranch Hand

Joined: Oct 05, 2010
Posts: 245

One solution would be to wrap your FileWriter object in a BufferedWriter object and then use a newLine() method after every write().
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46362
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

Joined: Sep 10, 2013
Posts: 11
Try replacing FileWriter with PrintWriter. PrintWriter has println methods which will print the output of each call on a new line.
I agree. Here's the link:
subject: Problem with writing texts in new line
It's not a secret anymore!