Example usage for java.io File delete

List of usage examples for java.io File delete

Introduction

In this page you can find the example usage for java.io File delete.

Prototype

public boolean delete() 

Source Link

Document

Deletes the file or directory denoted by this abstract pathname.

Usage

From source file:Main.java

public static int deleteBlankPath(String path) {
    File f = new File(path);
    if (!f.canWrite()) {
        return 1;
    }//w  w  w.  j  a va2 s .  co m
    if (f.list() != null && f.list().length > 0) {
        return 2;
    }
    if (f.delete()) {
        return 0;
    }
    return 3;
}

From source file:edu.unc.lib.dl.util.ZipFileUtil.java

/**
 * Create a temporary directory, unzip the contents of the given zip file to
 * it, and return the directory./*from www.j a v  a2 s.c om*/
 * 
 * If anything goes wrong during this process, clean up the temporary
 * directory and throw an exception.
 */
public static File unzipToTemp(File zipFile) throws IOException {
    // get a temporary directory to work with
    File tempDir = File.createTempFile("ingest", null);
    tempDir.delete();
    tempDir.mkdir();
    tempDir.deleteOnExit();
    log.info("Unzipping to temporary directory: " + tempDir.getPath());
    try {
        unzip(new FileInputStream(zipFile), tempDir);
        return tempDir;
    } catch (IOException e) {
        // attempt cleanup, then re-throw
        org.apache.commons.io.FileUtils.deleteDirectory(tempDir);
        throw e;
    }
}

From source file:edu.unc.lib.dl.util.ZipFileUtil.java

/**
 * Create a temporary directory, unzip the contents of the given zip file to
 * it, and return the directory.//from  w  ww.ja v a  2  s.  c  om
 * 
 * If anything goes wrong during this process, clean up the temporary
 * directory and throw an exception.
 */
public static File unzipToTemp(InputStream zipStream) throws IOException {
    // get a temporary directory to work with
    File tempDir = File.createTempFile("ingest", null);
    tempDir.delete();
    tempDir.mkdir();
    tempDir.deleteOnExit();
    log.debug("Unzipping to temporary directory: " + tempDir.getPath());
    try {
        unzip(zipStream, tempDir);
        return tempDir;
    } catch (IOException e) {
        // attempt cleanup, then re-throw
        org.apache.commons.io.FileUtils.deleteDirectory(tempDir);
        throw e;
    }
}

From source file:Main.java

public static void deleteFile(File file) {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        if (file.exists()) {
            if (file.isFile()) {
                file.delete();
            }/*from  w  w  w.j a  va  2s.  c o m*/

            file.delete();
        }
    }
}

From source file:Utils.java

/**
 * Recursively delete a file and all its contents.
 * /* w w w.j a v a 2s . co  m*/
 * @param root
 *          the root to delete
 */
public static void recursiveDelete(File root) {
    if (root == null) {
        return;
    }

    if (root.isDirectory()) {
        File[] files = root.listFiles();
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                if (file.isDirectory()) {
                    recursiveDelete(file);
                } else {
                    file.delete();
                }
            }
        }
    }
    root.delete();
}

From source file:Main.java

/**
 * replaces a list of files with another list of files, indicated by names
 * @param originalList//from   www . java2s.  c  o m
 * @param newList
 * @return
 */
public static void replaceFiles(ArrayList<String> originalList, ArrayList<String> newList)
        throws UnsupportedOperationException, OperationCanceledException {

    if (originalList.size() != newList.size())
        throw new UnsupportedOperationException();
    else {
        String name = null;
        for (int i = 0, size = originalList.size(); i < size; i++) {
            name = originalList.get(i);
            File f = new File(name);
            File newF = new File(newList.get(i));
            if (f.exists() && newF.exists()) {
                if (f.delete()) {
                    File temp = new File(name);
                    newF.renameTo(temp);
                } else {
                    throw new OperationCanceledException("Delete failed");
                }
            } else {
                throw new UnsupportedOperationException("Wrong lists of file names");
            }
        }
    }
}

From source file:Main.java

/**
 * recursively delete/*from w  w w  .ja  v  a2  s  .  c  o m*/
 *
 * @param dir
 * @throws java.io.IOException
 */
public static void deleteDirectoryRecursively(File dir) throws IOException {
    File[] files = dir.listFiles();
    if (files == null) {
        throw new IllegalArgumentException("not a directory: " + dir);
    }
    for (File file : files) {
        if (file.isDirectory()) {
            deleteDirectoryRecursively(file);
        }
        if (!file.delete()) {
            throw new IOException("failed to delete file: " + file);
        }
    }
}

From source file:Main.java

public static void deleteFile(String fileName) {
    File file = new File(fileName);
    if (file.exists()) {
        if (file.isDirectory()) {
            if (file.listFiles().length == 0) {
                file.delete();
            } else {
                for (File f : file.listFiles()) {
                    if (f.isDirectory()) {
                        deleteFile(f.getPath());
                    } else {
                        f.delete();/*from  w  w w  . j  a v a 2s  . c o  m*/
                    }
                }
                file.delete();
            }
        } else {
            file.delete();
        }
    }
}

From source file:Main.java

private static void copy(File srcFile, File targetFile) throws IOException {
    FileChannel src = null, dst = null;
    try {//from   w w w  .jav  a  2  s  . c o  m
        if (srcFile.exists() && (!targetFile.exists() || targetFile.delete())) {
            src = new FileInputStream(srcFile).getChannel();
            dst = new FileOutputStream(targetFile).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
        }
    } finally {
        try {
            if (src != null)
                src.close();
        } catch (IOException e) {
            Log.e("DIARY", "Failed to close resource.", e);
        }
        try {
            if (dst != null)
                src.close();
        } catch (IOException e) {
            Log.e("DIARY", "Failed to close resource.", e);
        }
    }
}

From source file:Main.java

public static void writeFile(InputStream in, File file) throws IOException {
    if (!file.getParentFile().exists())
        file.getParentFile().mkdirs();//ww w  .jav a2  s  . c  o  m

    if (file != null && file.exists())
        file.delete();

    FileOutputStream out = new FileOutputStream(file);
    byte[] buffer = new byte[1024 * 128];
    int len = -1;
    while ((len = in.read(buffer)) != -1) {
        out.write(buffer, 0, len);
    }
    out.flush();
    out.close();
    in.close();

}