Example usage for java.io File isDirectory

List of usage examples for java.io File isDirectory

Introduction

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

Prototype

public boolean isDirectory() 

Source Link

Document

Tests whether the file denoted by this abstract pathname is a directory.

Usage

From source file:Main.java

/**
 * Deletes the contents of {@code dir}. Throws an IOException if any file
 * could not be deleted, or if {@code dir} is not a readable directory.
 *///  w ww . j av a  2  s.  co  m
static void deleteContents(File dir) throws IOException {
    File[] files = dir.listFiles();
    if (files == null) {
        throw new IOException("not a readable directory: " + dir);
    }
    for (File file : files) {
        if (file.isDirectory()) {
            deleteContents(file);
        }
        if (!file.delete()) {
            throw new IOException("failed to delete file: " + file);
        }
    }
}

From source file:Util.java

public static boolean recursiveDeleteFile(File path) {
    if (path.exists()) {
        if (path.isDirectory()) {
            File[] files = path.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    recursiveDeleteFile(files[i]);
                } else {
                    files[i].delete();//from   w  w  w.  ja v a 2s.co m
                }
            }
        }
    }
    return (path.delete());
}

From source file:Main.java

/**
 * DOC bZhou Comment method "searchAllFile".
 * //w ww.ja v  a2s. c  om
 * @param result
 * @param parent
 * @param recursive
 * @param withFolder
 */
public static void searchAllFile(List<File> result, File parent, boolean recursive, boolean withFolder) {
    File[] files = parent.listFiles();
    if (files != null) {
        for (File file : files) {
            if (file.isDirectory() && recursive) {
                if (withFolder) {
                    result.add(file);
                }
                searchAllFile(result, file, recursive, withFolder);
            } else {
                result.add(file);
            }
        }
    } else {
        result.add(parent);
    }
}

From source file:Main.java

public static Enumeration getAllFilesIn(File dir) {
    File[] files;//from   ww w  .j  a v a  2s . c om
    if (dir.isDirectory()) {
        files = dir.listFiles(new FileFilter() {
            public boolean accept(File f) {
                if (f.isDirectory())
                    return false;
                return (f.getName().endsWith(".txt"));
            }
        });
        Arrays.sort(files);
    } else {
        files = new File[] { dir };
    }
    Vector vect = new Vector(files.length);
    for (int i = 0; i < files.length; ++i)
        vect.addElement(files[i]);
    return vect.elements();
}

From source file:Main.java

static boolean isFileUseful(File file) {
    return file != null && file.exists() && file.canRead() && !file.isDirectory();
}

From source file:Main.java

public static long getDirectorySize(File path) {
    if (!path.isDirectory()) {
        return path.length();
    }//from   www  .j a va  2s .  co m

    long size = 0;

    if (path.exists()) {
        File[] files = path.listFiles();

        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                size += getDirectorySize(files[i]);
            } else {
                size += files[i].length();
            }
        }
    }

    return size;
}

From source file:Main.java

public static String showOpenFile(String currentDirectoryPath, Component parent, final String filterRegex,
        final String filterDescription) {
    JFileChooser fileChooser = new JFileChooser(currentDirectoryPath);
    fileChooser.addChoosableFileFilter(new FileFilter() {
        private Pattern regexPattern = Pattern.compile(filterRegex);

        public boolean accept(File f) {
            if (f.isDirectory())
                return true;
            return regexPattern.matcher(f.getName()).matches();
        }/*from   ww w.j  a v a  2 s.c  om*/

        public String getDescription() {
            return filterDescription;
        }

    });
    fileChooser.showOpenDialog(parent);

    File choosedFile = fileChooser.getSelectedFile();
    if (choosedFile == null)
        return null;
    return choosedFile.getAbsolutePath();
}

From source file:Main.java

public static int getFolderSize(String relativePath) {
    int fileLength = 0;
    //      File dir = new File(path);
    File dir = creatSDDir(relativePath);
    if (dir.isDirectory()) {
        File[] files = dir.listFiles();
        for (File file : files) {
            fileLength += file.length();
        }//w w w . j  av  a  2 s.  c o  m
    } else {
        return -1;
    }
    return fileLength / 1024;
}

From source file:Main.java

static void deleteFile(File f, boolean toDeleteSelf) {
    if (f.isDirectory()) {
        for (File child : f.listFiles())
            deleteFile(child, true);/*www  .  j a v a2s .c  o m*/
    }
    if (toDeleteSelf)
        f.delete();
}

From source file:Main.java

public static boolean dirWritable(File dirFile) {
    try {/*from w  ww  .  j a v  a  2 s  .  com*/
        if (!dirFile.isDirectory())
            return false;
        File testFile = File.createTempFile("test", "tmp", dirFile);
        testFile.delete();
        return true;
    } catch (IOException e) {
        return false;
    }
}