Two Laptop Bag
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reg PrintWriter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reg PrintWriter" Watch "Reg PrintWriter" New topic

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 ?

class Test
public static void main(String args[])
try {
File f = new File("test12.txt");
FileWriter fw = new FileWriter(f);
PrintWriter pw = new PrintWriter(fw);
catch (Exception e)
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="" target="_blank" rel="nofollow"></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..

I agree. Here's the link:
subject: Reg PrintWriter
It's not a secret anymore!