Java File Path Delete deleteFile(String filePath)

Here you can find the source of deleteFile(String filePath)

Description

delete File

License

Open Source License

Declaration

public static void deleteFile(String filePath) throws IOException 

Method Source Code


//package com.java2s;
/* Copyright 2012, 2013 Unconventional Thinking
 *
 * This file is part of Hierarchy./*ww  w  .  ja v  a 2s . co  m*/
 *
 * Hierarchy is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * Hierarchy is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with Hierarchy.  
 * If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

import java.io.IOException;

public class Main {
    public static void deleteFile(String filePath) throws IOException {
        deleteFile(filePath, false);
    }

    public static void deleteFile(String filePath, boolean checkIfExists) throws IOException {

        File file = new File(filePath);

        if (!file.exists()) {
            if (checkIfExists) // don't throw error, just return
                return;
            else
                throw new IOException("The file does not exist:" + filePath);
        }
        if (!file.isFile())
            throw new IOException("The path given is not for a file: " + filePath);
        if (!file.canWrite())
            throw new IOException("The file to delete is not deletable: " + filePath);

        file.delete();

    }

    public static void deleteFile(File file) throws IOException {
        deleteFile(file, false);
    }

    public static void deleteFile(File file, boolean checkIfExists) throws IOException {

        if (!file.exists()) {
            if (checkIfExists) // don't throw error, just return
                return;
            else
                throw new IOException("The file does not exist:" + file.getCanonicalPath());
        }
        if (!file.isFile())
            throw new IOException("The path given is not for a file: " + file.getCanonicalPath());
        if (!file.canWrite())
            throw new IOException("The file to delete is not deletable: " + file.getCanonicalPath());

        file.delete();

    }
}

Related

  1. deleteFile(String filePath)
  2. deleteFile(String filePath)
  3. deleteFile(String filePath)
  4. deleteFile(String filePath)
  5. deleteFile(String filePath)
  6. deleteFile(String filePath)
  7. deleteFile(String filePathAndName)
  8. deleteFile(String filePathName)
  9. deleteFile(String p_filePath, boolean pb_recursive)