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:eu.digitisation.idiomaident.utils.CorpusFilter.java

private static HashMap<String, File> getFolderList(File parent) {
    File[] folders = parent.listFiles(new FilenameFilter() {
        @Override/*from   ww w  . j a  va 2  s  .com*/
        public boolean accept(File file, String name) {
            return file.isDirectory();
        }
    });

    HashMap<String, File> map = new HashMap<>();

    for (File folder : folders) {
        map.put(folder.getName(), folder);
    }

    return map;
}

From source file:io.github.azige.mages.Util.java

static List<Plugin> loadPluginsFromDirectory(File dir) {
    List<Plugin> list = new LinkedList<>();
    File[] files = dir.listFiles(new FilenameFilter() {

        @Override/* w w  w.  ja v  a  2 s .  c  o m*/
        public boolean accept(File dir, String name) {
            return name.endsWith(".groovy");
        }
    });
    if (files == null) {
        return list;
    }
    try {
        GroovyClassLoader loader = new GroovyClassLoader();
        loader.addClasspath(dir.getCanonicalPath());
        final int extLength = ".groovy".length();
        for (File f : files) {
            String name = f.getName();
            String script = FileUtils.readFileToString(f, "UTF-8");
            list.add(wrapPlugin(name.substring(0, name.length() - extLength),
                    loader.parseClass(script).newInstance()));
        }
    } catch (Exception ex) {
        throw new MagesException(ex);
    }
    return list;
}

From source file:Main.java

/**
 * List files in a null pointer safe way.
 * @param configDir the directory to list, may be null
 * @param filter the filter to match names against, may be null.
 * @return an array of files if no files match the filters the empty array is returned.
 *//*w  w w  .  j  a va 2s .  co m*/
public static File[] listFiles(File configDir, FileFilter filter) {
    final File[] files;
    if (configDir == null)
        files = null;
    else
        files = configDir.listFiles(filter);
    return files == null ? new File[0] : files;
}

From source file:Main.java

/**
 * Gets the number of cores available in this device, across all processors.
 * Requires: Ability to peruse the filesystem at "/sys/devices/system/cpu"
 * @return The number of cores, or 1 if failed to get result
 *//*from w w w. j  ava2  s.co m*/
public static int getNumCores() {
    try {
        //Get directory containing CPU info
        File dir = new File("/sys/devices/system/cpu/");
        //Filter to only list the devices we care about
        File[] files = dir.listFiles(new FileFilter() {

            @Override
            public boolean accept(File pathname) {
                //Check if filename is "cpu", followed by a single digit number
                return Pattern.matches("cpu[0-9]", pathname.getName());
            }

        });
        //Return the number of cores (virtual CPU devices)
        return files.length;
    } catch (Exception e) {
        //Default to return 1 core
        return 1;
    }
}

From source file:Main.java

public static File[] getMountedVolumesAsFile() {
    final String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { // we can read the External Storage...           
        //Retrieve the primary External Storage:
        final File primaryExternalStorage = Environment.getExternalStorageDirectory();

        //Retrieve the External Storages root directory:
        String externalStorageRootDir = null;
        if ((externalStorageRootDir = primaryExternalStorage.getParent()) == null) { // no parent...
            return (new File[] { primaryExternalStorage });
        } else {//from  w  w w  . java2  s  . co m
            final File externalStorageRoot = new File(externalStorageRootDir);
            final File[] files = externalStorageRoot.listFiles(new FilenameFilter() {

                @Override
                public boolean accept(File dir, String filename) {
                    // TODO Auto-generated method stub

                    File file = new File(dir, filename);
                    if (file.isDirectory() && file.canRead() && file.canWrite() && !file.isHidden()) {
                        return true;
                    }
                    return false;
                }

            }); //.listFiles();
            List<File> data = new ArrayList<File>();

            for (final File file : files) {
                if (file.isDirectory() && file.canRead() && file.canWrite() && !file.isHidden()
                        && (files.length > 0)) { // it is a real directory (not a USB drive)...
                    if (file.toString().equals(primaryExternalStorage.toString()))
                        data.add(file);
                    else {
                        if (!file.toString().contains("usb") || !file.toString().contains("USB")) {
                            data.add(file);
                        }
                    }
                }
            }
            return data.toArray(new File[data.size()]);
        }
    } else {
        // we cannot read the External Storage..., return null
        return null;
    }
}

From source file:de.tudarmstadt.ukp.experiments.argumentation.sequence.significance.SignificanceMain.java

/**
 * Test significance between all methods in the folder and prints them as a CSV table
 *
 * @param mainFolder         folder//from ww w.ja va  2s. c  o  m
 * @param folderResultPrefix prefix of folders to test, i.e. "cv_"
 * @throws IOException
 */
public static void compareMethods(File mainFolder, final String folderResultPrefix) throws IOException {
    File[] cvResults = mainFolder.listFiles(new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            return pathname.isDirectory() && pathname.getName().startsWith(folderResultPrefix);
        }
    });

    Table<String, String, Boolean> table = TreeBasedTable.create();

    for (int i = 0; i < cvResults.length; i++) {
        for (int j = 0; j < cvResults.length; j++) {
            if (i != j) {
                File one = cvResults[i];
                File two = cvResults[j];
                double pValue = TestSignificanceTwoFiles.compareId2OutcomeFiles(
                        new File(one, TOKEN_LEVEL_PREDICTIONS_CSV), new File(two, TOKEN_LEVEL_PREDICTIONS_CSV),
                        new OutcomeSuccessMapReaderSequenceTokenCSVImpl());

                // order of methods does not matter
                List<String> methods = new ArrayList<>();
                methods.add(one.getName());
                methods.add(two.getName());
                Collections.sort(methods);

                table.put(methods.get(0), methods.get(1), pValue < P_VALUE);

                // and make symmetric matrix
                table.put(methods.get(1), methods.get(0), pValue < P_VALUE);
            }
        }
    }

    System.out.println(tableToCsv(table));
}

From source file:com.l2jfree.tools.NewLineChecker.java

private static void parse(File f) throws IOException {
    if (f.isDirectory()) {
        for (File f2 : f.listFiles(FILTER))
            parse(f2);/* w  ww . j  av  a 2  s  . c o m*/
        return;
    }

    final String input = FileUtils.readFileToString(f);
    final List<String> inputLines = FileUtils.readLines(f);

    final int r = StringUtils.countMatches(input, "\r");
    final int n = StringUtils.countMatches(input, "\n");
    final int rn = StringUtils.countMatches(input, "\r\n");

    final char lastChar = input.charAt(input.length() - 1);
    boolean missingNewline = false;

    if (lastChar != '\r' && lastChar != '\n') {
        System.out.println("--- " + f.getCanonicalPath());
        System.out.println(lastChar);

        MISSING_NEWLINE++;

        missingNewline = true;
    }

    // fully "\r\n"
    if (r == n && r == rn && n == rn) {
        RN++;
        if (missingNewline)
            FileUtils.writeLines(f, inputLines, "\r\n");
        return;
    }

    // fully "\n"
    if (r == 0 && rn == 0) {
        N++;
        System.out.println("n " + f.getName());
        if (missingNewline)
            FileUtils.writeLines(f, inputLines, "\n");
        return;
    }

    System.out.println("--- " + f.getCanonicalPath());
    System.out.println("r: " + r);
    System.out.println("n: " + n);
    System.out.println("rn: " + rn);

    FileUtils.writeLines(f, inputLines, f.getName().endsWith(".sh") ? "\n" : "\r\n");

    // fully "\r"
    if (n == 0 && rn == 0) {
        R++;
        return;
    }

    // mixed
    MIXED++;
}

From source file:Main.java

/**
 * Gets the number of cores available in this device, across all processors.
 * Requires: Ability to peruse the filesystem at "/sys/devices/system/cpu"
 *
 * @return The number of cores, or 1 if failed to get result
 *///from  w w w  . j  a v a2s  .  co  m
public static int getNumCores() {
    try {
        //Get directory containing CPU info 
        File dir = new File("/sys/devices/system/cpu/");
        //Filter to only list the devices we care about 
        File[] files = dir.listFiles(new FileFilter() {

            @Override
            public boolean accept(File pathname) {
                //Check if filename is "cpu", followed by a single digit number 
                return Pattern.matches("cpu[0-9]", pathname.getName());
            }

        });
        //Return the number of cores (virtual CPU devices) 
        if (files != null)
            return files.length;
        return 1;
    } catch (Exception e) {
        e.printStackTrace();
        return 1;
    }
}

From source file:Main.java

/** 
 * Gets the number of cores available in this device, across all processors. 
 * Requires: Ability to peruse the filesystem at "/sys/devices/system/cpu" 
 * @return The number of cores, or 1 if failed to get result 
 *//*  w  w  w .  j  ava 2 s  .c om*/
public static int getNumCores() {
    try {
        //Get directory containing CPU info 
        File dir = new File("/sys/devices/system/cpu/");
        //Filter to only list the devices we care about 
        File[] files = dir.listFiles(new FileFilter() {

            @Override
            public boolean accept(File pathname) {
                //Check if filename is "cpu", followed by a single digit number 
                if (Pattern.matches("cpu[0-9]", pathname.getName())) {
                    return true;
                }
                return false;
            }

        });
        //Return the number of cores (virtual CPU devices) 
        return files.length;
    } catch (Exception e) {
        e.printStackTrace();
        return 1;
    }
}