How to check a file if it exists with a template in Java?

I have a directory, and inside it the files are called "a_id_XXX.zip" .

How to check if a file exists with id and File dir options?

+7
source share
6 answers

Pass the FileFilter (encoded here anonymously ) to listFiles() the File method, for example:

 File dir = new File("some/path/to/dir"); final String id = "XXX"; // needs to be final so the anonymous class can use it File[] matchingFiles = dir.listFiles(new FileFilter() { public boolean accept(File pathname) { return pathname.getName().equals("a_id_" + id + ".zip"); } }); 


As a method, it will look like this:

 public static File[] findFilesForId(File dir, final String id) { return dir.listFiles(new FileFilter() { public boolean accept(File pathname) { return pathname.getName().equals("a_id_" + id + ".zip"); } }); } 

and which you can name as follows:

 File[] matchingFiles = findFilesForId(new File("some/path/to/dir"), "XXX"); 

or just check availability,

 boolean exists = findFilesForId(new File("some/path/to/dir"), "XXX").length > 0 
+16
source

I created zip files named a_id_123.zip, a_id_124.zip, a_id_125.zip, a_id_126.zip and it looks like it works fine, but I'm not sure if this is the right answer for you. The output will be as follows if the files listed above exist.

  • found a_id_123.zip
  • found a_id_124.zip
  • found a_id_125.zip
  • found a_id_126.zip

     public static void main(String[] args) { String pathToScan = "."; String fileThatYouWantToFilter; File folderToScan = new File(pathToScan); // import -> import java.io.File; File[] listOfFiles = folderToScan.listFiles(); for (int i = 0; i < listOfFiles.length; i++) { if (listOfFiles[i].isFile()) { fileThatYouWantToFilter = listOfFiles[i].getName(); if (fileThatYouWantToFilter.startsWith("a_id_") && fileThatYouWantToFilter.endsWith(".zip")) { System.out.println("found" + " " + fileThatYouWantToFilter); } } } } 
+4
source

This decision is generalized to the Bohemian answer. It uses regular expressions and also replaces the inner class with the Java 8 lambda expression. Thanks @Bohemian for the original implementation.

 import java.io.File; public class FileFinder { public static void main(String[] args){ File directory = new File("D:\\tmp"); String id = "20140430104033"; for (File f : findFilenamesWithId(id, directory)){ System.out.println(f.getAbsoluteFile()); } } /** Finds files in the specified directory whose names are formatted as "a_id_ID.zip" */ public static File[] findFilenamesWithId(String ID, File dir) { return findFilenamesMatchingRegex("^a_id_" + ID + "\\.zip$", dir); } /** Finds files in the specified directory whose names match regex */ public static File[] findFilenamesMatchingRegex(String regex, File dir) { return dir.listFiles(file -> file.getName().matches(regex)); } } 
+1
source

Java 7 has good support for pattern matching (PathMatcher) and recursive directory navigation (Files.walkFileTree ()). In the context of the original question, this page in the Oracle Java documentation is a good start:

File Search: http://docs.oracle.com/javase/tutorial/essential/io/find.html

0
source

All Articles