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:DirList2.java

public static FilenameFilter filter(final String regex) {
    // Creation of anonymous inner class:
    return new FilenameFilter() {
        private Pattern pattern = Pattern.compile(regex);

        public boolean accept(File dir, String name) {
            return pattern.matcher(new File(name).getName()).matches();
        }// w ww.  ja  va 2s  .c  om
    }; // End of anonymous inner class
}

From source file:Main.java

public static void traverseFolder(Map<String, File> fileMap, File rootFolder, File folderInCheck) {
    if (folderInCheck.isFile()) {
        int length = (int) rootFolder.getAbsolutePath().length();
        String pathKey = folderInCheck.getAbsolutePath().substring(length);
        fileMap.put(pathKey, folderInCheck);
        return;/*from w  w w. ja va 2 s  . co m*/
    }

    String[] names = folderInCheck.list(new FilenameFilter() {
        // for filtering some files, such as ".xml"
        public boolean accept(File dir, String name) {
            return true;
        }
    });

    if (names == null || names.length == 0) {
        return;
    }

    File[] files = folderInCheck.listFiles();
    if (files == null || files.length == 0) {
        return;
    }

    for (File child : files) {
        traverseFolder(fileMap, rootFolder, child);
    }
}

From source file:Main.java

public static String prepareFilePathForVideoSaveWithDraftUri(Uri draftUri) {
    String draftPath = draftUri.getPath();
    String draftMediaDirPath = draftPath.substring(0, draftPath.length() - 5);
    File draftMediaDir = new File(draftMediaDirPath);
    if (!draftMediaDir.exists()) {
        draftMediaDir.mkdirs();/* www.j a  v a2  s .  com*/
    }
    String[] files = draftMediaDir.list(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String filename) {
            return filename.endsWith("-0.mp4") || filename.endsWith("-a.mp4");
        }
    });
    List<String> filePaths = Arrays.asList(files);
    Collections.sort(filePaths, new Comparator<String>() {
        @Override
        public int compare(String lhs, String rhs) {
            return rhs.compareTo(lhs);
        }
    });
    if (filePaths.size() > 0) {
        for (String file : filePaths) {
            return new File(draftMediaDir, file.substring(0, file.length() - 6) + ".mp4").getAbsolutePath();
        }
    }
    return new File(draftMediaDir, generateRandomFilename("mp4")).getAbsolutePath();
}

From source file:Main.java

/**
 * Returns album IDs containing at least one cached track.
 * @return Albums IDs/*from w w  w  .  j  ava2s.  c o  m*/
 */
public static Set<String> getCachedAlbumSet() {
    File cacheDir = getMusicCacheDir();
    File[] albumList = cacheDir.listFiles();
    Set<String> output = new HashSet<>();
    for (File album : albumList) {
        if (album.list(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String filename) {
                return filename.endsWith(".complete");
            }
        }).length > 0) {
            output.add(album.getName());
        }
    }
    return output;
}

From source file:Main.java

private static Set<File> getExpectationFiles(String dir) {
    Set<File> expectSet = new HashSet<File>();
    File[] files = new File(dir).listFiles(new FilenameFilter() {
        // ignore obviously temporary files
        public boolean accept(File dir, String name) {
            return !name.endsWith("~") && !name.startsWith(".");
        }// w  w w.j av a2s .  c o m
    });
    if (files != null) {
        expectSet.addAll(Arrays.asList(files));
    }
    return expectSet;
}

From source file:Main.java

static public ArrayList<String> getSavedGestureNames(Context context) {

    ArrayList<String> gestureNames = new ArrayList<>();

    File[] files = context.getFilesDir().listFiles(new FilenameFilter() {
        @Override/* ww  w .  jav a  2 s  .c  o m*/
        public boolean accept(File dir, String filename) {
            return filename.endsWith(GESTURE_NAME_SUFFIX);
        }
    });

    //return the files without the suffix
    for (File file : files) {
        gestureNames.add(file.getName().substring(0, file.getName().indexOf(GESTURE_NAME_SUFFIX)));
    }

    return gestureNames;
}

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  . j  a  va2s  . 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<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:com.tw.go.plugin.material.artifactrepository.yum.exec.RepoqueryCacheCleaner.java

public static void performCleanup() throws IOException {
    File[] cacheFiles = new File("/var/tmp/").listFiles(new FilenameFilter() {
        public boolean accept(File dir, String name) {
            String currentUser = System.getProperty("user.name") != null ? System.getProperty("user.name")
                    : "go";
            return name.startsWith(String.format("yum-%s-", currentUser));
        }//from   w w w  . j  a v  a 2 s. c  o m
    });
    for (File cacheFile : cacheFiles) {
        FileUtils.forceDelete(cacheFile);
    }
}

From source file:Main.java

public static File[] listBackups(File backupPath) {

    if (!backupPath.isDirectory() || !backupPath.canRead()) {
        return new File[0];
    }//from w w w . j  a  v a 2  s.c  om
    File[] files = backupPath.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String filename) {
            return (new File(dir, filename)).isFile() && filename.endsWith(".backup");
        }
    });

    if (files != null) {
        Arrays.sort(files, new Comparator<File>() {
            @Override
            public int compare(File s1, File s2) {
                return s2.getName().compareTo(s1.getName());
            }
        });
        return files;
    } else {
        return new File[0];
    }
}

From source file:Main.java

public static void deleteFromInternalStorage(Context context, final String contains) throws IOException {
    File file = context.getFilesDir();
    FilenameFilter filter = new FilenameFilter() {
        @Override//  ww w.  j  a va 2  s  .co  m
        public boolean accept(File dir, String filename) {
            return filename.contains(contains);
        }
    };
    File[] files = file.listFiles(filter);
    if (files != null) {
        for (File f : files) {
            //noinspection ResultOfMethodCallIgnored
            if (!f.delete()) {
                throw new IOException("Error while deleting files");
            }
        }
    }
}