aspose file tools*
The moose likes I/O and Streams and the fly likes How to add a line break in a text file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "How to add a line break in a text file?" Watch "How to add a line break in a text file?" New topic
Author

How to add a line break in a text file?

Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

Hi all,

I am trying to write data into a text file using java. I have created a form and it should write the details entered by a SINGLE user in a single line(for an example, all the data entered by user1 should save in line number one and all the details entered by user2 should save in line number two). I have used escaped chracters but no good. Here is a part of my code



Please help me


Are you better than me? Then please show me my mistakes..
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4529
    
    5

How did you check whether the file has line breaks or not? and in what OS?


luck, db
There are no new questions, but there may be new answers.
Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

I opened the file, and had a look. I am using Windows.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

And Notepad, right?

Where Linux uses just \n for line breaks, Windows officially uses the \r\n combination. Proper text editors can also handle \n, but Notepad isn't a proper text editor. Any \r or \n will be displayed as a little block ([]), and only \r\n is a line break.

That's why you usually shouldn't use \n directly when working with files that will be used on Windows. System.getProperty("line.separator") will return the proper String for this (so "\r\n" for Windows), and java.util.Formatter and the methods that use it (String.format, PrintStream.printf, etc) will use the proper String when you use "%n" in the pattern.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

COOL! That combination of two escape characters worked fine!
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2848
    
  11

Glad you got that working! However, I think Rob's point was you should use System.getProperty("line.separator") to be platform independent. I often set a constant somewhere and then just use the constant from then on. Java's ability to run on multiple platforms without recompiling is one of its major benefits, but you have to sort of cooperate with it to make that work.
Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

I understand. But I have very little time to complete this, so I jumped in to the fastest solution. Thanks a lot for all of you. I really appreciate your help.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3053
    
  36

Jonathan Haybok,
Your post was moved to a new topic.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to add a line break in a text file?