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 String[] getSecondaryDirs() {
    List<String> ret = new ArrayList<String>();
    String secondaryStorageString = System.getenv("SECONDARY_STORAGE");
    if (secondaryStorageString != null && !secondaryStorageString.trim().isEmpty()) {
        String[] dirs = secondaryStorageString.split(":");

        for (String dir : dirs) {
            File file = new File(dir);
            if (file.isDirectory() && file.canWrite()) {
                ret.add(dir);//  ww w .  j a va2 s. c o  m
            }
        }

        if (ret.isEmpty())
            return null;
        else
            return ret.toArray(new String[ret.size()]);

    } else {
        return null;
    }
}

From source file:Main.java

public static void deleteFile(File file) {
    if (file == null) {
        return;//w  w w.  ja v a  2  s  . c o m
    }
    if (file.isDirectory() && file.listFiles() != null) {
        for (File item : file.listFiles()) {
            deleteFile(item);
        }
    }
    try {
        file.delete();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return;
}

From source file:Main.java

public static void clearSystemCache(Context context) {
    try {/*  w w  w. j  a v a 2  s.  c om*/
        File dir = context.getCacheDir();
        if (dir != null && dir.isDirectory()) {
            deleteDir(dir);
        }
    } catch (Exception e) {
    }
}

From source file:Main.java

/**
 * Return the directory where the generated tlogs logging files are stored.
 * @return File to the tlogs directory//from  ww  w .j  ava2  s. co  m
 */
private static File getTLogsDirectory(Context context) {
    File tlogDir = new File(context.getExternalFilesDir(null), DIRECTORY_TLOGS);
    if (!tlogDir.isDirectory()) {
        tlogDir.mkdirs();
    }

    return tlogDir;
}

From source file:Main.java

public static void copyDir(String src, String dst) {
    try {// www .  ja  v  a2s. c  om
        File fileSrc = new File(src);
        if (!fileSrc.exists()) {
            return;
        }
        File[] filelist = fileSrc.listFiles();
        File fileDst = new File(dst);
        if (!fileDst.exists()) {
            fileDst.mkdirs();
        }
        for (File f : filelist) {
            if (f.isDirectory()) {
                copyDir(f.getPath() + "/", dst + f.getName() + "/");
            } else {
                copyFile(f.getPath(), dst + f.getName());
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Returns the path only (without file name).
 * @param file/*from w  w w.j  a v a  2s.  c  o m*/
 * @return
 */
public static File getPathWithoutFilename(File file) {
    if (file != null) {
        if (file.isDirectory()) {
            // no file to be split off. Return everything
            return file;
        } else {
            String filename = file.getName();
            String filepath = file.getAbsolutePath();

            // Construct path without file name.
            String pathwithoutname = filepath.substring(0, filepath.length() - filename.length());
            if (pathwithoutname.endsWith("/")) {
                pathwithoutname = pathwithoutname.substring(0, pathwithoutname.length() - 1);
            }
            return new File(pathwithoutname);
        }
    }
    return null;
}

From source file:ReflectionUtil.java

private static void getPackageNamesFromDir(File base, File dir, List<String> pkgs) {
    boolean foundClass = false;
    for (File file : dir.listFiles()) {
        if (file.isDirectory()) {
            getPackageNamesFromDir(base, file, pkgs);
        } else if (!foundClass && file.getName().endsWith(".class")) {
            foundClass = true;/*w  w  w.j  a va 2  s . com*/
            String pkg = "";
            file = dir;
            while (!file.equals(base)) {
                if (!"".equals(pkg)) {
                    pkg = "." + pkg;
                }
                pkg = file.getName() + pkg;
                file = file.getParentFile();
            }
            if (!pkgs.contains(pkg)) {
                pkgs.add(pkg);
            }
        }
    }
}

From source file:Main.java

public static void deleteFileDirectory(File file) {
    if (!file.exists()) {
        return;//  w ww  . j a  v  a2  s.c o m
    }
    if (file.isDirectory()) {
        File[] files = file.listFiles();
        if (files == null || files.length == 0) {
            file.delete();
            return;
        }
        for (int i = 0, length = files.length; i < length; i++) {
            files[i].delete();
        }
        file.delete();
    }
}

From source file:Main.java

private static boolean checkFsWritable() {
    String directoryName = Environment.getExternalStorageDirectory().toString() + "/DCIM";
    File directory = new File(directoryName);
    if (!directory.isDirectory()) {
        if (!directory.mkdirs()) {
            return false;
        }/*from   w  w w .  j a v a 2 s  .  c  o  m*/
    }
    File f = new File(directoryName, ".probe");
    try {
        if (f.exists()) {
            f.delete();
        }
        if (!f.createNewFile()) {
            return false;
        }
        f.delete();
        return true;
    } catch (IOException ex) {
        return false;
    }
}