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(FileFilter filter) 

Source Link

Document

Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.

Usage

From source file:Main.java

public static File[] listFiles(File f) {
    File[] list;//ww  w . j a  va  2  s.co m
    if (f.isDirectory()) {
        list = f.listFiles(filter);
        if (list != null) {
            Arrays.sort(list, comparator);
        }
    } else {
        list = new File[] {};
    }
    return list;
}

From source file:Main.java

public static int getNumCores() {
    try {/*from  w ww. jav a2s.  c om*/
        File dir = new File("/sys/devices/system/cpu/");
        File[] files = dir.listFiles(new FileFilter() {

            @Override
            public boolean accept(File pathname) {
                return Pattern.matches("cpu[0-9]", pathname.getName());
            }

        });
        return files.length;
    } catch (Exception e) {
        e.printStackTrace();
        return 1;
    }
}

From source file:Main.java

public static int cpuNums() {
    int nums = 1;
    try {/*  w ww.j a v a 2 s  .  c o  m*/
        File file = new File("/sys/devices/system/cpu/");
        File[] files = file.listFiles(new FileFilter() {
            @Override
            public boolean accept(File arg0) {
                if (Pattern.matches("cpu[0-9]", arg0.getName())) {
                    return true;
                }
                return false;
            }
        });
        nums = files.length;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return nums;
}

From source file:Main.java

public static File[] getAppFolders() {
    File odk = new File(getOdkFolder());

    File[] results = odk.listFiles(new FileFilter() {

        @Override//from   w ww.  ja v a 2 s  .co m
        public boolean accept(File pathname) {
            if (!pathname.isDirectory())
                return false;
            return true;
        }
    });

    return results;
}

From source file:Main.java

/**
 * Get all temp files./*from w w  w  .  j a va  2  s  .co m*/
 *
 * @return The list of existing temp files.
 */
public static File[] getTempCameraFiles() {
    File tempDir = getTempCameraFolder();

    File[] files = tempDir.listFiles(new FileFilter() {
        @Override
        public boolean accept(@NonNull final File file) {
            return file.isFile();
        }
    });
    if (files == null) {
        files = new File[0];
    }
    Arrays.sort(files);

    return files;
}

From source file:Main.java

public static int getCPUCores() {
    //Strategy 1.
    //The value may less than real value.
    //int cores_runtime = Runtime.getRuntime().availableProcessors();

    //Strategy 2.
    int cores = 1;
    class CpuFilter implements FileFilter {
        @Override//  w ww. j  a  v a  2s  .c  om
        public boolean accept(File pathname) {
            return Pattern.matches("cpu[0-9]", pathname.getName());
        }
    }
    try {
        File dir = new File("/sys/devices/system/cpu/");
        File[] files = dir.listFiles(new CpuFilter());
        cores = files.length;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return cores;
}

From source file:Main.java

public static Enumeration getAllFilesIn(File dir) {
    File[] files;/*www  . j  a v a2s .  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:net.orfjackal.dimdwarf.server.ApplicationLoader.java

private static File[] listJarsInDirectory(File dir) {
    return dir.listFiles(new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return name.endsWith(".jar");
        }/*from ww w  . ja v a 2 s . c  o  m*/
    });
}

From source file:Main.java

public static void removeUnusedTiles(Context mContext, final ArrayList<String> usedTiles) {
    // remove all files are stored in the tile path but are not used
    File folder = new File(mContext.getFilesDir(), TILE_PATH);
    File[] unused = folder.listFiles(new FilenameFilter() {

        @Override//from  w w w .  j a v  a  2  s .  c  om
        public boolean accept(File dir, String filename) {
            return !usedTiles.contains(filename);
        }
    });

    if (unused != null) {
        for (File f : unused) {
            f.delete();
        }
    }
}

From source file:Main.java

public static void getFileList(final List<File> fileList, final File root, FileFilter filter) {
    final File[] list = root.listFiles(filter);

    if (list == null) {
        return;//from   w  ww  .j  a v  a2 s. com
    }

    for (final File f : list) {
        if (f.isDirectory()) {
            fileList.add(f);
            getFileList(fileList, f, filter);
        } else {
            fileList.add(f);
        }
    }
}