aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reg PrintWriter Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Reg PrintWriter" Watch "Reg PrintWriter" New topic
Author

Reg PrintWriter

babu sharath
Ranch Hand

Joined: Jul 06, 2006
Posts: 42
Hi ,
I am using java1.4
I write following code.
Problem is : In the end new file is created , but it is empty nothing is written
in that file . Why ? line1 and line2 should be placed in the file , why it is not happening ?

import java.io.*;
class Test
{
public static void main(String args[])
{
try {
File f = new File("test12.txt");
if(!f.exists())
f.createNewFile();
FileWriter fw = new FileWriter(f);
PrintWriter pw = new PrintWriter(fw);
pw.println("line1");
pw.println("line2");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
prashanth kumar
Ranch Hand

Joined: Mar 22, 2004
Posts: 162
you need to flush() it..


SCJP1.5(86%)<br />SCWCD1.4(95%)<br />SCBCD1.3(92%)<br />IBM 252
Dan Polak
Ranch Hand

Joined: Nov 06, 2006
Posts: 32
Yeah and close


<a href="http://www.dantheman.pl" target="_blank" rel="nofollow">http://www.dantheman.pl</a>
Saurabh Vyas
Ranch Hand

Joined: Sep 02, 2003
Posts: 72
You need to close FileWriter and PrintWriter than only the content in the file will be commited and than line1 and line2 would be available in the file.
prashanth kumar
Ranch Hand

Joined: Mar 22, 2004
Posts: 162
Are we saying here that "close()" method call is mandatory for stuff to be written to file??I dont think so..Its just a best practise..

Cheers
Prashanth
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Reg PrintWriter
 
Similar Threads
move file A to file B
Writting into two files
Ocurrio un error:java.io.IOException: Stream closed
About PrintWriter with println and write
How to insert new line a text file