• 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

a filewriter problem...

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
can someone tell me if there is a way by which a filewriter deletes the entire contents of an existing file and start writing fresh from the 1st line of that file?basically in my applet i am using a couple of text files and i want that when the applet is destroyed then either the text files should be entirely deleted or at least their contents should be deleted in their entirety..please tell me how can this be done..
thanks a lot in advance
karan
PS- please help ASAP,its kinda urgent
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi karan
instead of going for filewriter , u can go for a FileOutputStream .
here's the code.
================
should be given within the constructor
Button save=new Button("Save");
save.addActionListener(this);
this code is for a save button .
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==save)
{
String s1,s2,s3;
FileDialog fd=new FileDialog(h,"Save",FileDialog.SAVE);
fd.show();
try
{
s1=fd.getDirectory();
s2=fd.getFile();
f1=new File(s1,s2);
FileOutputStream fos1=new FileOutputStream(f1);
s3=ta.getText();
byte b1[]=s3.getBytes();
fos1.write(b1);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
 
karan, chopra
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi manjrekar,
thanks for the reply,but i was wondering if there was a way by which i could just delete the entire file when i dispose the applet.i have heard that there is a command called File.delete..but i dont know how to use it..if u know it then can u please send me a piece of code for the usage of the same?
thanks again
regards
karan
 
manjerekar annajirao
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi karan
i wonder y u want to delete the file. but still i will help u out.
there are two methods which can help u to delete the file
in java.awt.File class we have
1. boolean delete();
2. void deleteOnExit();
i think this will help u

manjerekar

 
karan, chopra
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi manjrekar,
thanks again for the reply..i tried the file.delete option and it threw some exception which i have attached in my previous post under the subject of "file deletion problem"..can u please take a look at it and tell me what is the problem..please...
regards
karan
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey manjerekar,
on the code snipplet u posted here, what is the ta varable?
s3=ta.getText();
thanks!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic