Home arrow static arrow Java Programming [Archive] - new line or carriage return through FileWriter
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - new line or carriage return through FileWriter
This topic has 2 replies on 1 page.

Posts:4
Registered: 7/21/04
new line or carriage return through FileWriter  
Aug 2, 2004 11:00 AM



 
Hello !

I am using a java.io.FileWriter object to write some stuff to a given File object at runtime.
Everything works except I can't figure how to create new lines in the resulting file : all I am wirting in it appears stuck together, even if my String contain \n or \r characters.

I use java.io.FileWriter.write(String s) method

Thanks for any help !

Olivier
 

Posts:19,725
Registered: 9/26/01
Re: new line or carriage return through FileWriter  
Aug 2, 2004 11:04 AM (reply 1 of 2)



 
Wrap the FileWriter with a PrintWriter, and use println to write "lines" of text according to the platform it is running on.
 

Posts:14,142
Registered: 99-04-02
Re: new line or carriage return through FileWriter  
Aug 2, 2004 11:51 AM (reply 2 of 2)



 
as warnerja said...
but just so's you know... if you are on Windows, you need \r\n, both in that order. You can use the line.separator system property to get the proper separator in a generic way. That's what PrintWriter does internally.

Windows: \r\n
Unix: \n
Mac: \r (well, that's older Macs, OSX might use \n now... but I'm not 100% sure)
 
This topic has 2 replies on 1 page.