• 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

filewriter problem..please help ASAP.....

 
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
 
author
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you simply want to retain the original file (in place) and wipeout the content and write anew, just specify false for the append mode when you create the FileWriter. Like so.

You'll see how this does just that if you create a large file called text.txt and then run this. The remaining file will only include the "Hello World".
If you just want to have some temp file type of behavior, then explore the java.io.File class, particularly the createTempFile() and the deleteOnExit() methods. I hope this helps.
Sean
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sean,
I tried your code, but it did not work as what you said. Can you explane?
Thanks! Mindy

Originally posted by Sean MacLean:
[B]If you simply want to retain the original file (in place) and wipeout the content and write anew, just specify false for the append mode when you create the FileWriter. Like so.

You'll see how this does just that if you create a large file called text.txt and then run this. The remaining file will only include the "Hello World".
If you just want to have some temp file type of behavior, then explore the java.io.File class, particularly the createTempFile() and the deleteOnExit() methods. I hope this helps.
Sean[/B]


 
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
hey sean,
thanks a lot man..it worked and solved my purpose..
regards
karan
public static void main( String[] args ) {
try {
FileWriter fw = new FileWriter( "text.txt", false );
fw.write( "Hello world" );
fw.flush();
}
catch( IOException e ) {}
}
}
[/CODE]
You'll see how this does just that if you create a large file called text.txt and then run this. The remaining file will only include the "Hello World".
If you just want to have some temp file type of behavior, then explore the java.io.File class, particularly the createTempFile() and the deleteOnExit() methods. I hope this helps.
Sean[/B]
 
Are you here to take over the surface world? Because this tiny ad will stop you!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic