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 long getDirectorySize(File f) {
    if (f == null || !f.exists() || !f.isDirectory()) {
        return 0;
    }/*from  w  ww .ja v  a 2 s.co  m*/
    long size = 0;
    File[] flist = f.listFiles();
    if (flist != null) {
        for (int i = 0; i < flist.length; i++) {
            if (flist[i].isDirectory()) {
                size = size + getDirectorySize(flist[i]);
            } else {
                size = size + flist[i].length();
            }
        }
    }
    return size;
}

From source file:Main.java

public static boolean isFilesReady(Context context) {
    File hpDir = getSDDir(context);
    if (!hpDir.exists() || !hpDir.isDirectory()) {
        return false;
    }//w w  w .j av a 2  s  .  c  om
    File hp = new File(hpDir, "hp48");
    File rom = new File(hpDir, "rom");
    File ram = new File(hpDir, "ram");
    return hp.exists() && hp.length() > 0 && rom.exists() && rom.length() > 0 && ram.exists()
            && ram.length() > 0;
}

From source file:Main.java

public static void recursiveFileRemoval(File root) {
    if (!root.delete()) {
        if (root.isDirectory()) {
            File[] files = root.listFiles();
            if (files != null) {
                for (File f : files) {
                    recursiveFileRemoval(f);
                }//from ww  w.ja  va 2s.  c  om
            }
        }
    }
}

From source file:Main.java

public static File[] listBackups(File backupPath) {

    if (!backupPath.isDirectory() || !backupPath.canRead()) {
        return new File[0];
    }//from  www  .j  av a 2s. co  m
    File[] files = backupPath.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String filename) {
            return (new File(dir, filename)).isFile() && filename.endsWith(".backup");
        }
    });

    if (files != null) {
        Arrays.sort(files, new Comparator<File>() {
            @Override
            public int compare(File s1, File s2) {
                return s2.getName().compareTo(s1.getName());
            }
        });
        return files;
    } else {
        return new File[0];
    }
}

From source file:com.intropro.prairie.utils.FileUtils.java

public static List<String> readLineInDirectory(File file) throws IOException {
    List<String> result = new LinkedList<>();
    for (String child : file.list()) {
        File childFile = new File(file, child);
        if (childFile.isDirectory()) {
            result.addAll(readLineInDirectory(childFile));
        } else {/*from w  w  w.j a v a  2 s  . c  om*/
            result.addAll(IOUtils.readLines(new FileInputStream(childFile)));
        }
    }
    return result;
}

From source file:Main.java

private static void deleteFilesByDirectory(File directory) {
    if (directory != null && directory.exists() && directory.isDirectory()) {
        for (File item : directory.listFiles()) {
            if (item.isDirectory()) {
                deleteFilesByDirectory(item);
            }//from  w  w  w . j  a v a  2  s.  c  o  m
            item.delete();
        }
        directory.delete();
    }
}

From source file:Main.java

public static String readDeviceUUID(String devicePath) {
    String uuid = null;//from  w  w w. j  av  a  2  s  . c om
    File root = new File(devicePath);
    if (root.isDirectory()) {
        File uFile = new File(devicePath, ".uuid");
        FileReader reader = null;
        try {
            char[] buffer = new char[36];
            reader = new FileReader(uFile);
            reader.read(buffer, 0, buffer.length);
            uuid = new String(buffer);
            Log.d(TAG, "readDeviceUUID uuid:" + uuid);
            return uuid;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    return uuid;
}

From source file:Main.java

/**
 * Deletes the specified diretory and any files and directories in it
 * recursively./*from   ww  w.j ava 2s .c  o  m*/
 * 
 * @param dir The directory to remove.
 * @throws IOException If the directory could not be removed.
 */
public static void deleteDir(File dir) throws IOException {
    if (!dir.isDirectory()) {
        throw new IOException("Not a directory " + dir);
    }

    File[] files = dir.listFiles();
    for (int i = 0; i < files.length; i++) {
        File file = files[i];

        if (file.isDirectory()) {
            deleteDir(file);
        } else {
            boolean deleted = file.delete();
            if (!deleted) {
                throw new IOException("Unable to delete file" + file);
            }
        }
    }

    dir.delete();
}

From source file:Main.java

private static void deleteDirectoryContent(String directoryPath) {
    File dir = new File(directoryPath);
    if (dir.exists() && dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            File file = new File(dir, children[i]);
            if (file.isDirectory()) {
                deleteDirectoryContent(file.getAbsolutePath());
            } else {
                file.delete();//w  w w . ja va  2 s .co  m
            }
        }
        dir.delete();
    }
}

From source file:Main.java

private static boolean checkFsWritable(String dir) {

    if (dir == null)
        return false;

    File directory = new File(dir);

    if (!directory.isDirectory()) {
        if (!directory.mkdirs()) {
            return false;
        }/*from  w w w. ja  va 2 s  .  c  om*/
    }

    File f = new File(directory, ".keysharetestgzc");
    try {
        if (f.exists()) {
            f.delete();
        }
        if (!f.createNewFile()) {
            return false;
        }
        f.delete();
        return true;

    } catch (Exception e) {
    }
    return false;

}