Example usage for java.io File listFiles

List of usage examples for java.io File listFiles

Introduction

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

Prototype

public File[] listFiles() 

Source Link

Document

Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.

Usage

From source file:Main.java

public static long getFolderSize(File file) throws Exception {
    long j = 0;//w  w  w. jav a 2s . co  m
    File[] listFiles = file.listFiles();
    for (int i = 0; i < listFiles.length; i++) {
        j = listFiles[i].isDirectory() ? j + getFolderSize(listFiles[i]) : j + listFiles[i].length();
    }
    return j / 1048576;
}

From source file:Main.java

private static void findJavaFiles(String projectDir, List<String> javaFilePaths) {
    File file = new File(projectDir);
    File[] files = file.listFiles();
    if (files == null || files.length == 0) {
        return;/*www  .ja  va  2 s  . c  om*/
    }
    for (File f : files) {
        if (f.isDirectory()) {
            findJavaFiles(f.getAbsolutePath(), javaFilePaths);
        } else {
            if (f.getAbsolutePath().endsWith(".java")) {
                javaFilePaths.add(f.getAbsolutePath());
            }
        }
    }
}

From source file:Main.java

private static void iterateFolder(File folder, List<File> files) {
    File flist[] = folder.listFiles();
    files.add(folder);//  w w w . j  a  va  2  s .c  o  m
    if (flist == null || flist.length == 0) {
        files.add(folder);
    } else {
        for (File f : flist) {
            if (f.isDirectory()) {
                iterateFolder(f, files);
            } else {
                files.add(f);
            }
        }
    }
}

From source file:Main.java

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

From source file:Main.java

public static void deleteCache(File cacheDir) {
    try {/* www .j  ava2  s  .c o  m*/

        File[] files = cacheDir.listFiles();

        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()) {
                files[i].delete();
            }
            if (files[i].isDirectory()) {
                deleteCache(files[i]);
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:andrew.addons.NextFile.java

public static String findExt(String folder, String file) {
    File dir = new File(folder);
    File[] list = dir.listFiles();
    if (list != null) {
        for (File fil : list) {
            if (file.equals(FilenameUtils.removeExtension(fil.getName())))
                return fil.getName();
        }/*from w  w  w .  ja  va 2s.  co m*/
    }
    return "";
}

From source file:Main.java

public static String[] ListFile() {

    File file = new File("/sdcard/xUtils/");
    File[] f = file.listFiles();
    if (f != null) {
        String Path[] = new String[f.length];
        for (int i = 0; i < f.length; i++)

        {//from   w  w  w  .ja va 2  s. c  o  m

            Path[i] = f[i].getPath();
        }
        return Path;
    } else {
        return null;
    }

}

From source file:Main.java

private static void deleteDirectorySync(File dir) {
    try {//  w  w  w  . j  a va 2  s. c o m
        File[] files = dir.listFiles();
        if (files != null) {
            for (File file : files) {
                String fileName = file.getName();
                if (!file.delete()) {
                    Log.e(TAG, "Failed to remove " + file.getAbsolutePath());
                }
            }
        }
        if (!dir.delete()) {
            Log.w(TAG, "Failed to remove " + dir.getAbsolutePath());
        }
        return;
    } catch (Exception e) {
        Log.e(TAG, "Failed to remove old libs, ", e);
    }
}

From source file:Main.java

public static void deleteDirectory(File dir) {
    if (dir.isDirectory()) {
        File[] listFiles = dir.listFiles();
        for (int i = 0; i < listFiles.length; i++) {
            deleteDirectory(listFiles[i]);
        }//from  w ww .  j a v a  2s.  co m
    }
    dir.delete();
}

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  www  .j  a v a2 s . c  o  m
                }
            } else {
                if (file.getName().toLowerCase().contains(fileType.toLowerCase())) {
                    listFile.add(file);
                }
            }
        }
    }
}