• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to empty a file  RSS feed

 
George Brown
Ranch Hand
Posts: 919
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I was wondering, can anyone suggest a quicker way to zeroise a file than the following:

of course, there's RandomAccessFile, but with that you have to construct, setLength, and then close, which of course is more lines to maintain and is slower:

(in my tests, the FileWriter example is 5 times quicker).
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if it's any quicker (and the underlaying process of new FileWriter probably does it anyway)
( new File( filename ) ).delete( ) ;
That should rid the file without creating a new stream to as does
new FileWriter( filename ) ;
If you don't need a new OutputStream, I'd test this way for performance.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!