• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to do it??

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi folks!
check out the following code...

import java.io.*;
class test1
{
public static void main(String a[])throws Exception
{
BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("c:\\java\\test.txt",true)));
bf.write("r"+'\n'+"r"+'\n'+"r"+'\n'+"r"+'\n'+"r"+'\n'+"r");
bf.flush();
}
}
im creating this file on my disk and writing some text into it. when i opened the file test.txt, there were 6 rs written in it separated by a black dot(representing next line character). i want to have each r printed on new line rather than on 1 line separated by that dot. can u tell me how to do it???. thanx
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
windows uses different line termination charaters than UNIX:
windows uses: \r\n
UNIX uses: \n
The BufferedWriter contains the method newLine, which is described in the API as:
A newLine() method is provided, which uses the platform's own notion of line separator as defined by the system property line.separator. Not all platforms use the newline character ('\n') to terminate lines. Calling this method to terminate each output line is therefore preferred to writing a newline character directly.
The API is at:
http://java.sun.com/j2se/1.4/docs/api/java/io/BufferedWriter.html
Hope this helps. CHeers, Neil
 
Neil Laurance
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition consider using a PrintWriter instead, then you could do:
pw.println("r");
Which will add the correct end-of-line charcter(s) for your operating system.
Cheers, Neil
[ November 06, 2002: Message edited by: Neil Laurance ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic