• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to do it??  RSS feed

 
reehan ishaque
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • 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
 
Neil Laurance
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • 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
  • 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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!