Java File Delete nio secureDelete(File file)

Here you can find the source of secureDelete(File file)

Description

Securely deletes a file.

License

Open Source License

Parameter

Parameter Description
file the file to delete.

Exception

Parameter Description
FileNotFoundException if the file does not exist.
IOException if the delete cannot be completed.

Declaration

public static void secureDelete(File file) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009-2016 Black Rook Software
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 ******************************************************************************/

import java.io.*;

import java.nio.channels.FileLock;

import java.util.Arrays;

public class Main {
    /**/*from w  ww. j  a v a  2s.c  o  m*/
     * Securely deletes a file. Overwrites its data with zeroes and its filename
     * a bunch of times before it finally deletes it. Places an exclusive
     * lock on the file as it is getting deleted. The length of time taken
     * depends the file's length and the efficiency of the medium that contains it.
     * <p>
     * <b>DISCLAIMER:</b> Black Rook Software makes NO CLAIMS of COMPLETE SECURITY
     * concerning deletions of files using this method. This method is provided AS-IS.
     * It is reasonably secure insofar as deleting file content and headers as Java
     * may allow. This does not alter dates of files.
     * @param file the file to delete.
     * @throws FileNotFoundException if the file does not exist.
     * @throws IOException if the delete cannot be completed.
     * @since 2.9.0
     */
    public static void secureDelete(File file) throws IOException {
        secureDelete(file, 1);
    }

    /**
     * Securely deletes a file. Overwrites its data and its filename
     * a bunch of times before it finally deletes it. Places an exclusive
     * lock on the file as it is getting deleted. The length of time taken
     * depends the file's length and the efficiency of the medium that contains it. 
     * <p>
     * <b>DISCLAIMER:</b> Black Rook Software makes NO CLAIMS of COMPLETE SECURITY
     * concerning deletions of files using this method. This method is provided AS-IS.
     * It is reasonably secure insofar as deleting file content and headers as Java
     * may allow. This does not alter dates of files directly.
     * @param file the file to delete.
     * @param passes the amount of passes for overwriting this file.
     * The last pass is always zero-filled. If less than 1, it is 1.
     * @throws FileNotFoundException if the file does not exist.
     * @throws IOException if the delete cannot be completed.
     * @since 2.9.0
     */
    public static void secureDelete(File file, int passes) throws IOException {
        passes = passes < 1 ? 1 : passes;
        boolean bitval = passes == 1 ? false : (passes % 2) == 0;

        // Overwrite.
        RandomAccessFile raf = new RandomAccessFile(file, "rws");
        FileLock lock = raf.getChannel().lock();
        byte[] buffer = new byte[65536];
        while (passes-- > 0) {
            Arrays.fill(buffer, (byte) (bitval ? 0xFF : 0x00));
            long end = raf.length();
            raf.seek(0L);
            long n = 0L;
            while (n < end) {
                raf.write(buffer, 0, Math.min(buffer.length, (int) (end - n)));
                n = raf.getFilePointer();
            }
        }
        lock.release();
        raf.close();

        // Overwrite filename.
        String newName = null;
        char[] namebuf = new char[file.getName().length()];
        char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ9876543210".toCharArray();
        for (char c : alphabet) {
            Arrays.fill(namebuf, c);
            newName = new String(namebuf);
            File ren = new File(file.getParent() + File.separator + newName);
            file.renameTo(ren);
            file = ren;
        }

        file.delete();
    }

    /**
     * Attempts to close a {@link Closeable} object.
     * If the object is null, this does nothing.
     * @param c the reference to the closeable object.
     * @since 2.3.0
     */
    public static void close(Closeable c) {
        if (c == null)
            return;
        try {
            c.close();
        } catch (IOException e) {
        }
    }

    /**
     * Attempts to close an {@link AutoCloseable} object.
     * If the object is null, this does nothing.
     * @param c the reference to the AutoCloseable object.
     * @since 2.31.1
     */
    public static void close(AutoCloseable c) {
        if (c == null)
            return;
        try {
            c.close();
        } catch (Exception e) {
        }
    }
}

Related

  1. forceDelete(final File file)
  2. forceDeletion(File fileToDelete)
  3. recDeleteDirFile(File f)
  4. recursiveDelete(File parent)
  5. safeDeleteFile(final String fileName)