Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to add a line break in a text file?

 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How did you check whether the file has line breaks or not? and in what OS?
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I opened the file, and had a look. I am using Windows.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
COOL! That combination of two escape characters worked fine!
 
Greg Charles
Sheriff
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 4588
302
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonathan Haybok,
Your post was moved to a new topic.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic