aspose file tools
The moose likes Java in General and the fly likes filewriter problem..please help ASAP..... Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "filewriter problem..please help ASAP....." Watch "filewriter problem..please help ASAP....." New topic
Author

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

karan, chopra
Ranch Hand

Joined: Jun 05, 2001
Posts: 115
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
Sean MacLean
author
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
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
Mindy Wu
Ranch Hand

Joined: Jan 12, 2001
Posts: 121
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

Joined: Jun 05, 2001
Posts: 115
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]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: filewriter problem..please help ASAP.....
 
Similar Threads
manipulating text file
File writer issue
How can I display the contents of a file
filewriter problem...urgent...please help..
a filewriter problem...