Example usage for java.io FilenameFilter FilenameFilter

List of usage examples for java.io FilenameFilter FilenameFilter

Introduction

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

Prototype

FilenameFilter

Source Link

Usage

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//  w w w.  ja 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

/**
 * Obtains a list of files that live in the specified directory and match the glob pattern.
 *///from w w w .j a v  a  2s  .com
public static String[] getFiles(File dir, String glob) {
    String regex = globToRegex(glob);
    final Pattern pattern = Pattern.compile(regex);
    String[] result = dir.list(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            Matcher matcher = pattern.matcher(name);
            return matcher.matches();
        }
    });
    Arrays.sort(result);

    return result;
}

From source file:com.krzysztofzabinski.publication1.Remover.java

public static void readInput() {
    File inputDirectory = new File("input/.");
    inputFiles = inputDirectory.listFiles(new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return name.toLowerCase().endsWith("train.csv");
        }//ww w . java2  s .c o m
    });

    for (File file : inputFiles) {
        List<String> tmpList = new ArrayList<>();

        BufferedReader reader = null;
        String line;
        try {

            reader = new BufferedReader(new FileReader(file.getAbsolutePath()));
            while ((line = reader.readLine()) != null) {
                tmpList.add(line);
            }

        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                }
            }
        }

        inputList.add(tmpList);
    }

}

From source file:Main.java

static String composeFilesString(final String dirPath, final List<String> subDirPaths, final String pattern,
        int skip) {
    final StringBuilder sb = new StringBuilder();
    final List<File> allFiles = new ArrayList<>();
    final FilenameFilter filter = new FilenameFilter() {
        @Override/*from  www .j  a  v a 2s .  c  om*/
        public boolean accept(File dir, String name) {
            return name.matches(pattern);
        }
    };
    for (final String subDirPath : subDirPaths) {
        final File subDir = new File(dirPath, subDirPath);
        final File[] files = subDir.listFiles(filter);
        if (files == null) {
            throw new RuntimeException(String.format("%s directory does not exist", subDir.getPath()));
        }
        Arrays.sort(files);
        Collections.addAll(allFiles, files);
    }
    final int m;
    final int n;
    if (skip >= 0) {
        m = skip;
        n = allFiles.size();
    } else {
        m = 0;
        n = allFiles.size() + skip;
    }
    for (int i = m; i < n; i++) {
        final File file = allFiles.get(i);
        if (sb.length() > 0) {
            sb.append(' ');
        }
        sb.append(file.getPath());
    }
    return sb.toString();
}

From source file:Main.java

public static String[] getMountedVolumes() {
    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 String[] { "ONLY A SINGLE VOLUME HAS BEEN DETECTED!",
                    (Environment.isExternalStorageRemovable() ? "(REMOVABLE SD-CARD)" : "(INTERNAL STORAGE)")
                            + " PRIMARY STORAGE: " + primaryExternalStorage });
        } else {/*  www  .  j  a  v a  2  s .  c o  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<String> data = new ArrayList<String>();
            if (files.length > 1) {
                data.add("MULTIPLE VOLUMES HAS BEEN DETECTED!");
                data.add("Enumerating detected volumes . . .");
            } else {
                data.add("ONLY A SINGLE VOLUME HAS BEEN DETECTED!");
            }

            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((Environment.isExternalStorageRemovable() ? "(REMOVABLE SD-CARD)"
                                : "(INTERNAL Memory)") + " PRIMARY STORAGE: " + file.getAbsolutePath());
                    else {
                        data.add(((file.toString().contains("usb") || file.toString().contains("USB"))
                                ? "MOUNTED USB"
                                : "MOUNTED") + " STORAGE: " + file.getAbsolutePath());
                    }
                }
            }
            return data.toArray(new String[data.size()]);
        }
    } else {
        // we cannot read the External Storage..., return null
        return null;
    }

}

From source file:gov.nih.nci.ncicb.tcga.dcc.qclive.common.util.DirectoryListerImpl.java

/**
 * Convenience method to statically get list of files in dir.
 * @param dirName the directory in which to look
 * @return array of File objects/*ww  w  .  j a va2 s  . c o m*/
 */
public static File[] getFilesInDir(final String dirName) {
    final File dir = new File(dirName);
    // It is also possible to filter the list of returned files.
    // This does not return any files that start with `.'.
    final FilenameFilter filter = new FilenameFilter() {
        public boolean accept(final File dir, final String name) {
            return !name.startsWith(".");
        }
    };
    return dir.listFiles(filter) != null ? dir.listFiles(filter) : null;
}

From source file:eu.digitisation.idiomaident.utils.CorpusFilter.java

private static HashMap<String, File> getFolderList(File parent) {
    File[] folders = parent.listFiles(new FilenameFilter() {
        @Override//  w w  w. j a v  a 2s  .c o  m
        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:net.itransformers.idiscover.discoverylisteners.TopologyDeviceLogger.java

public static void main(String[] args) throws FileNotFoundException, JAXBException {
    String path = "tmp1";
    File dir = new File(path);
    String[] files = dir.list(new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return (name.startsWith("device") && name.endsWith(".xml"));
        }/*w w  w.ja v  a2s .  co m*/
    });
    TopologyDeviceLogger logger = new TopologyDeviceLogger(path);
    String host = "10.33.0.5";
    String snmpROComm = "public";
    Map<String, String> resourceParams = new HashMap<String, String>();
    resourceParams.put("community", snmpROComm);
    resourceParams.put("version", "1");
    Resource resource = new Resource(host, null, resourceParams);

    for (String fileName : files) {
        FileInputStream is = new FileInputStream(path + File.separator + fileName);
        DiscoveredDeviceData discoveredDeviceData = null;
        try {
            discoveredDeviceData = JaxbMarshalar.unmarshal(DiscoveredDeviceData.class, is);
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String deviceName = fileName.substring("device-".length(), fileName.length() - ".xml".length());
        logger.handleDevice(deviceName, null, discoveredDeviceData, resource);
    }

}

From source file:com.modelon.oslc.adapter.fmi.integration.FMUConnector.java

public static File[] finder(String dirName, String fileExtension) {
    File dir = new File(dirName);
    return dir.listFiles(new FilenameFilter() {
        public boolean accept(File dir1, String filename) {
            return filename.endsWith(fileExtension);
        }/*  w ww  .  j  a va  2 s .  c  o m*/
    });
}

From source file:eu.sisob.uma.restserver.FileSystemManager.java

/**
 *
 * @param code_task_folder_dir/* w  w  w.j av  a 2 s. c  o m*/
 * @param filename_prefix
 * @param filename_ext
 * @return
 */
public static File getFileIfExists(File code_task_folder_dir, final String filename_prefix,
        final String filename_ext) {

    List<File> list = null;
    if (code_task_folder_dir.exists()) {

        list = Arrays.asList(code_task_folder_dir.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.startsWith(filename_prefix) && name.endsWith(filename_ext);
            }
        }));
    }

    return (list != null && list.size() == 1 ? list.get(0) : null);

}