File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes Class for safe deletion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Class for safe deletion" Watch "Class for safe deletion" New topic

Class for safe deletion

Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 439
Hi all,

I have made this simple class for safe deletion on windows operating system. I have read that your deleted files are even recovered after completely formatting your hard disk. Following deletion guidelines, I have made a a program that deletes files but first writing over the file many 7 times or as many times as the user has mentioned.

I want your participation to make it more foolproof. Maybe some member here is using windows and he is going to format his computer. I want him to test this with a small text file, say "new.txt" on the drive he is going to format. Later he may try to recover the file using some powerful data recovering software and see if the data is recovered. Other member may give some idea...

The text file "new.txt" may contain:
"JAVA" only. It makes it 4 bytes.

Usage is:

D:>java DataDeleter c:\new.txt 13

This will overwrite 13 times.

Here is the code

[ December 08, 2007: Message edited by: Maki Jav ]

Help gets you when you need it!
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

You can't do this kind of thing this way. The 13 copies of your file might all use different blocks on the filesystem!

The only way to do this is to use low-level OS-specific I/O calls to talk directly to the disk itself, addressing the specific sectors that you want to secure-delete. You're not going to be able to do this in Java.

It might work if you use RandomAccessFile, keep seeking back to the beginning and overwriting the data without closing the file. But it also might not; there's no guarantee the data will actually go out to the disk.

Finally, note that you're mixing up two different issues here: one is that deleting a file generally doesn't delete the data, on most system; it just removes a directory entry, leaving the data intact. This is why something like "Norton Undelete" had such a successful run, back when the FAT filesystem made this very simple to fix.

Then there's the other issue which is that even if data is overwritten, magnetic traces of the original data can still remain. This data is hard to recover, and requires special tools (possibly including opening up the disk in a clean room.) But for industrial espionage and other high-stakes data recovery attempts, it is sometimes used.

[Jess in Action][AskingGoodQuestions]
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 439
SO it means that I will raf.flush() each time after a write.

Other things; well that is why I am here. Maybe someone has the intention to delete/format his drive and he also have a super duper tool of data recovery too. That member may participate in this.

Does Unix/Linux or any other OS makes the hard drive clean? If data once written is always on the disk, how does one get full space to use after a format.


Maki Jav
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
RandomAccessFile does not have flush() method. And even if RandomAccessFile had such method it wouldn't be enough for this purpose.

You need FileChannel and its force() method instead of flush.

To increase security use random data or at least a different pattern for each overwriting pass.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
To be fair, you can use a RandomAccessFile, then force the associated FileChannel by calling getChannel().force(). Or call getFD().sync(), which should provide the same effect. It seems a rather poor design that these methods were not included directly in RandomAccessFile, but they're still available. They're just hidden.

"I'm not back." - Bill Harding, Twister
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 439

Well thank you for these replies. Please don't say that it is a bad design jim. maybe they had in mind that local writing was not needed!
But I still did not grasp the difference between Java writing to a file and
the real writing to the disk. How does the disk then store the file written by Java?

Now I wait for someone who will actually format his drive after using
program and then try to recover data with the most powerful tool that he has

Maki Jav
[ December 22, 2007: Message edited by: Maki Jav ]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Maki Jav:
But I still did not grasp the difference between Java writing to a file and
the real writing to the disk. How does the disk then store the file written by Java?

It is not specific to Java. The same would be true of any ordinary user application writing to disk via the ordinary APIs.

Most operating systems have facilities to delay or buffer data written to disk by ordinary APIs. They do this for performance. And, potentially, if many writes to the same part of the disk occur, in a short time, the operating system may never actually write some of those to the real disk; in the worst case, only the last would really get written.

One can also imagine that disk drives themselves may implement write buffers. They certainly implement read buffers.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
I agree. Here's the link:
subject: Class for safe deletion
It's not a secret anymore!