aspose file tools
The moose likes Java in General and the fly likes flushing output to different lines Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "flushing output to different lines" Watch "flushing output to different lines" New topic
Author

flushing output to different lines

rudolf hitler
Ranch Hand

Joined: Feb 07, 2003
Posts: 34
Hi
i have aproblem with putputtin data to a txt file
i have 3 lines for e.g
My name is
My age is
My nationality is
i want all these 3 lines ot be printed in a text file on different lines i used the following
FileWriter fw=new Filewriter("news_Archives.txt",true);
PrintWriter pw=new PrintWriter(fw,true);
But i realised tht the file writer appends to end of the file whereas the print writer flushes o/p to new line.But in this case file writer overrides print writer and my output just gets printed on one line.How do i acheive printing on different lines
Amol Desai
Ranch Hand

Joined: Jan 29, 2003
Posts: 82
Hi,
You can use the following piece of code
String str1="My name is";
String str2="My age is";
String str3="My Nationality is";
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("E:/UserDirectory/out.txt"));

bw.write(str1);
bw.newLine();
bw.write(str2);
bw.newLine();
bw.write(str3);
bw.newLine();

bw.flush();
bw.close();
} catch (Exception e)
{
e.printStackTrace();
}
hope this helps
 
IntelliJ Java IDE
 
subject: flushing output to different lines
 
Threads others viewed
Writting into two files
Creating a new file using PrintWriter.
Data reading/output from file
Scaneer question
Java IO question
IntelliJ Java IDE