• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reg PrintWriter

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
}
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you need to flush() it..
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah and close
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic