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

public static boolean deleteDir(@NonNull File dir) {
    if (dir != null && dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }/*  w  ww . j a v a 2s  . c o  m*/
        }
    }
    if (dir == null) {
        return false;
    }
    return dir.delete();
}

From source file:Main.java

/**
 0: number of files/*from   w w  w .j a va 2s.  c  om*/
 1: total length
 2: number of directories
 */
public static void getDirSize(final File f, final long[] l) {
    if (f.isFile()) {
        l[0]++;
        l[1] += f.length();
    } else {
        Stack<File> stk = new Stack<>();
        stk.add(f);
        File fi = null;
        File[] fs;
        while (stk.size() > 0) {
            fi = stk.pop();
            fs = fi.listFiles();
            for (File f2 : fs) {
                if (f2.isDirectory()) {
                    stk.push(f2);
                    l[2]++;
                } else {
                    l[0]++;
                    l[1] += f2.length();
                }
            }
        }
    }
}

From source file:com.gargoylesoftware.js.CodeUpdater.java

private static void process(final File dir, final boolean isMain) throws IOException {
    for (final File file : dir.listFiles()) {
        if (file.isDirectory()) {
            process(file, isMain);//from   w w  w .j  av  a2  s  . c  o  m
        } else if (file.getName().endsWith(".java") && !file.getName().equals("package-info.java")) {
            processFile(file, isMain);
        }
    }
}

From source file:Main.java

public static List<File> searchFileFrom(String rootPath, String type) {

    listFile.clear();//from www  . ja  v a2  s.  co  m
    fileType = type;

    File rootFile = new File(rootPath);
    if (rootFile.isDirectory()) {
        searchDir(rootFile);
    }

    return listFile;
}

From source file:Main.java

private static void searchDir(File dirFile) {
    File[] files = dirFile.listFiles();
    if (files != null) {
        for (File file : files) {
            if (file.isDirectory()) {
                if (file.getName().subSequence(0, 1).equals(".") || file.getName().equals("Android")
                        || file.getPath().equalsIgnoreCase("/storage/emulated")) {

                } else {
                    searchDir(file);/*from   ww w .ja  v a  2s. com*/
                }
            } else {
                if (file.getName().toLowerCase().contains(fileType.toLowerCase())) {
                    listFile.add(file);
                }
            }
        }
    }
}

From source file:Main.java

public static void deleteCache(Context context) {
    try {/* www .  j  a  va2s . c  om*/
        File dir = context.getCacheDir();
        if (dir != null && dir.isDirectory()) {
            deleteDir(dir);
        }
    } catch (Exception e) {
    }
}

From source file:Main.java

public static void innerListFiles(Collection<File> files, File directory, FileFilter filter) {
    File[] found = directory.listFiles();
    if (found != null) {
        for (File aFound : found) {
            if (aFound.isDirectory()) {
                innerListFiles(files, aFound, filter);
            } else {
                File[] found2 = directory.listFiles(filter);
                Collections.addAll(files, found2);
            }/*from  w  ww .  j  a  v a2 s . c  o  m*/
        }
    }
}

From source file:Main.java

public static Collection<File> listFiles(File directory, String suffix) {
    final String _suffix = "." + suffix;
    FileFilter filter = new FileFilter() {
        @Override/* w w  w  . j av a2  s.c  o m*/
        public boolean accept(File file) {
            if (file.isDirectory())
                return true;
            String name = file.getName();
            int endLen = _suffix.length();
            if (name.regionMatches(true, name.length() - endLen, _suffix, 0, endLen)) {
                return true;
            }
            return false;
        }
    };
    if (!directory.isDirectory()) {
        throw new IllegalArgumentException("Parameter 'directory' is not a directory");
    }

    if (filter == null) {
        throw new NullPointerException("Parameter 'fileFilter' is null");
    }
    Collection<File> files = new LinkedList<File>();
    innerListFiles(files, directory, filter);
    return files;
}

From source file:Main.java

/**
 * Return the absolute path to the hub database.
 *
 * @return path The path to the db as a string
 *///w ww  .ja v a  2  s  . c o  m
private static String getDbPath() {
    if (Debug)
        Log.i(TAG, "getDBPath() called.");

    String path = Environment.getExternalStorageDirectory().getPath() + "/" + BASE_DIR;

    File dbDir = new File(path);
    if (!dbDir.isDirectory()) {
        try {
            if (Debug)
                Log.i(TAG, "Trying to create " + path);
            dbDir.mkdirs();
        } catch (Exception e) {
            final Writer result = new StringWriter();
            final PrintWriter printWriter = new PrintWriter(result);
            e.printStackTrace(printWriter);
            Log.e(TAG, result.toString());
        }
    }
    return path;
}

From source file:Main.java

private static void deleteContent(File dir, boolean deleteDir) {
    if (dir.isDirectory()) {
        File[] files = dir.listFiles();
        for (File file : files) {
            deleteContent(file, true);//from  ww  w.  j av  a2s. com
        }
    }
    if (deleteDir)
        dir.delete();
}