| 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
|
 |
 |
|
|
subject: Reg PrintWriter
|
|
|