Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

File Shredding & deleteOnExit()  RSS feed

 
Isaac Hewitt
Ranch Hand
Posts: 191
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This above method attempts to wipe a given file. The files I want wiped are created as temp files. The temp files are created alright because I can see them in the Windows TMP folder. Before adding the wipe method to my class, the temp files would always delete on exit. Now they don't, yet as "wiped" files they cannot be opened by any program and therefore are rendered useless (if not already useless). I still would like to have them deleted after exiting the program. Any thoughts? Thanks you.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The usual reason for a file not being deleted is that you're using Windows and some code didn't close the file properly.

I see you're satisfying the first condition, but I don't see that you're satisfying the second. However I don't know much about NIO so maybe there's more you have to do to stop locking the file.

You might want to get one of those Windows applications which let you see which applications are locking which files and use it to run some tests.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're closing the FileChannel, but I don't know if that also closes the RandomAccessFile. Try changing your code slightly:
Also notice how I added a try-finally block. This will ensure that the closing will always be done, even if an IOException occurs.

Another note: you are using a MappedByteBuffer which allows writing directly. Try changing your code a bit more:
And one final note: your code works with ints, so your file size is limited to 2GB files. If your file size exceeds Integer.MAXIMUM then numBytes may become negative. You should consider looping, each time shredding the next block of size (int)Math.min(BLOCK_SIZE, remainingBytes) with long remainingBytes starting out as rwChannel.size() and decreasing with BLOCK_SIZE each time. Don't use BLOCK_SIZE = Integer.MAX_VALUE because it's just about impossible to allocate that much memory for the JVM.
 
Isaac Hewitt
Ranch Hand
Posts: 191
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime,
Thanks for modifying my code. I will do some research & experimenting, then post the results in few days.
 
Isaac Hewitt
Ranch Hand
Posts: 191
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After having done some research and having experimented, I have found that System.gc() is the only thing to work at deleting these files. Since the file shredding method of my program is called at shutdown, and garbage collection occurs immediately after, there is no risk to the program. The temp files are kept in a List<File>. At shutdown, the list is iterated through, and each file is wiped. After the iteration loop, and the very last thing, the garbage collector is called. It succeeds in deleting every single tmp file generated after the wipe method, every single time it has been tested.
 
loh evans
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for some of the advice and codes. But i still don't understand. I still a beginner in Java programming. Is it possible that you send me the codes?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!