• Post Reply Bookmark Topic Watch Topic
  • New Topic

Starting a new line with FileWriter class

 
Vopli Vidoplyasova
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created a simple test for FileWriter class, here's the code:

The problem is, it writes all the text in one line. What can I do to make it begin a new line with every cycle of the loop? I tried playing with \n, but it doesn't seem to work. Would anybody please help me figure this out?
Thank you.
 
Eric Daly
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't remember exactly what each one means, but if you add + "\r\n" I think that should solve your problem. I know that on Windows systems you need both characters in the text files, not just \n, which does work fine if you're just using the command prompt, but not for file writing. On other systems, you may only need "\n".
Another thing I've seen is this, because it works regardless of which system you're running on:
System.getProperty( "line.separator" )
or in your case:

[ August 25, 2008: Message edited by: Eric Daly ]
 
Vopli Vidoplyasova
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just tried it, both "\r\n" and System.getProperty("line.separator") work just fine. Thanks Eric!
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Slava Golovachenko:
Just tried it, both "\r\n" and System.getProperty("line.separator") work just fine.


Glad you have it working. Using

is more transportable. Some systems (not sure how many current ones) don't keep the same CR followed by LF as terminator. the getProperty() will work everywhere, freeing programmers to chase bugs elsewhere.
 
Vopli Vidoplyasova
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a great tip, thanks Pat!
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use the format method of String class to get the platform-specific line separator:

[ August 25, 2008: Message edited by: Garrett Rowe ]
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jut for information....
Instead of using Filewriter, we should use either BufferedWrier or PrintWriter( java 5).

BufferedWriter provides newLine() method which make it possible to create plateform specific new line character automatically.
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if it is a simple text file, try the java.util.Formatter class.
 
Macario Garcia
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for this one.

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi "Hands Down".

Welcome to JavaRanch :-)

Can you please have a quick look at your PMs regarding an important administrative matter?

Thanks,
Steve
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!