FileDocCategorySizeDatePackage
SecureDelete.javaAPI DocExample1054Mon Feb 13 14:29:40 GMT 2006None

SecureDelete

public class SecureDelete extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

    
     File file = new File(args[0]);
     if (file.exists()) {
     SecureRandom random = new SecureRandom();
     RandomAccessFile raf = new RandomAccessFile(file, "rw");
     FileChannel channel = raf.getChannel();
     MappedByteBuffer buffer 
       = channel.map(FileChannel.MapMode.READ_WRITE, 0, raf.length());
     // overwrite with zeros
     while (buffer.hasRemaining()) {
       buffer.put((byte) 0);
     }
     buffer.force();
     buffer.rewind();
     // overwrite with ones
     while (buffer.hasRemaining()) {
       buffer.put((byte) 0xFF);
     }
     buffer.force();
     buffer.rewind();
     // overwrite with random data; one byte at a time
     byte[] data = new byte[1];
     while (buffer.hasRemaining()) {
       random.nextBytes(data);
       buffer.put(data[0]);
     }
     buffer.force();
     file.delete(); 
     }