Java Delete File deleteFile(final String dirname)

Here you can find the source of deleteFile(final String dirname)

Description

delete File

License

BSD License

Parameter

Parameter Description
dirname a parameter

Declaration

public static boolean deleteFile(final String dirname) 

Method Source Code

//package com.java2s;
/*--------------------------------------------------------
 * Copyright (c) 2011, The Dojo Foundation
 * This software is distributed under the "Simplified BSD license",
 * the text of which is available at http://www.winktoolkit.org/licence.txt
 * or see the "license.txt" file for more details.
 *--------------------------------------------------------*/

import java.io.File;

public class Main {
    /**//from   w w w  .  j  a  va  2 s  .  c o  m
     * @param dirname
     * @return
     */
    public static boolean deleteFile(final String dirname) {
        // System.out.println("deleteFile: " + dirname);
        final File f = new File(dirname);
        if (f.exists()) {
            return deleteR(f);
        }
        return false;
    }

    /**
     * @param path
     * @return
     */
    private static boolean deleteR(final File path) {
        if (path.exists()) {
            final File[] files = path.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    deleteR(files[i]);
                } else {
                    final boolean isFileDeleted = files[i].delete();
                    if (!isFileDeleted) {
                        return false;
                    }
                }
            }
        }
        final boolean isDeleted = path.delete();
        return isDeleted;
    }

    /**
     * @param dirname
     * @return
     */
    public static boolean isDirectory(final String dirname) {
        // System.out.println("isDirectory: " + dirname);
        final File f = new File(dirname);
        if (f.exists() && f.isDirectory() && f.canWrite()) {
            return true;
        }
        return false;
    }
}

Related

  1. deleteFile(final File f, final int attempts, final long sleepTime)
  2. deleteFile(final File file)
  3. deleteFile(final File file)
  4. deleteFile(final File file)
  5. deleteFile(final File file)
  6. deleteFile(Object fileObj)
  7. deleteFile(String archiveFilename)
  8. deleteFile(String dest)
  9. deleteFile(String f)