• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

How to add a line break in a text file?

 
Ranch Hand
Posts: 507
Netbeans IDE Oracle Java
  • 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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • 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
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I opened the file, and had a look. I am using Windows.
 
Sheriff
Posts: 21783
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
COOL! That combination of two escape characters worked fine!
 
Sheriff
Posts: 3034
12
Mac IntelliJ IDE Python VI Editor Java
  • 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
Netbeans IDE Oracle Java
  • 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.
 
Sheriff
Posts: 6377
1124
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonathan Haybok,
Your post was moved to a new topic.
 
Whatever you say buddy! And I believe this tiny ad too:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!