List of usage examples for org.apache.commons.io.filefilter SuffixFileFilter SuffixFileFilter
public SuffixFileFilter(List suffixes)
From source file:varioustests.swingworkertests.SearchForWordWorker.java
@Override protected Integer doInBackground() throws Exception { // The number of instances the word is found int matches = 0; /*//from ww w . ja va2 s . com * List all text files under the given directory using the Apache IO library. This process cannot be * interrupted (stopped through cancellation). That is why we are checking right after the process whether * it was interrupted or not. */ publish("Listing all text files under the directory: " + directory); final List<File> textFiles = new ArrayList<>( FileUtils.listFiles(directory, new SuffixFileFilter(".txt"), TrueFileFilter.TRUE)); SearchForWordWorker.failIfInterrupted(); publish("Found " + textFiles.size() + " text files under the directory: " + directory); for (int i = 0, size = textFiles.size(); i < size; i++) { /* * In order to respond to the cancellations, we need to check whether this thread (the worker thread) * was interrupted or not. If the thread was interrupted, then we simply throw an InterruptedException * to indicate that the worker thread was cancelled. */ SearchForWordWorker.failIfInterrupted(); // Update the status and indicate which file is being searched. final File file = textFiles.get(i); publish("Searching file: " + file); /* * Read the file content into a string, and count the matches using the Apache common IO and Lang * libraries respectively. */ final String text = FileUtils.readFileToString(file); matches += StringUtils.countMatches(text, word); // Update the progress setProgress((i + 1) * 100 / size); } // Return the number of matches found return matches; }
From source file:za.co.massdosage.scrobble.FileScrobbler.java
List<ScrobbleData> extractScrobbles(File scrobbleFolder) throws CannotReadException, IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException { List<ScrobbleData> scrobbles = new ArrayList<>(); File folders[] = scrobbleFolder.listFiles((FileFilter) DirectoryFileFilter.INSTANCE); if (folders != null) { for (File folder : folders) { scrobbles.addAll(extractScrobbles(folder)); }/*from w w w.j a va 2 s. c o m*/ } FileFilter fileFilter = new AndFileFilter(HiddenFileFilter.VISIBLE, new SuffixFileFilter(supportedFileTypes)); File files[] = scrobbleFolder.listFiles(fileFilter); if (files != null) { List<File> sortedFiles = Arrays.asList(files); Collections.sort(sortedFiles, new FileNameComparator()); ScrobbleData scrobbleData; for (Iterator<File> iterator = sortedFiles.iterator(); iterator.hasNext(); scrobbles .add(scrobbleData)) { File file = iterator.next(); scrobbleData = extractScrobble(file); } } return scrobbles; }