• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

how to do it??

 
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
 
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 ]
 
knowledge is the difference between drudgery and strategic action -- tiny ad
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic