How to check a file if exists with wildcard in Java?

前端 未结 7 663
谎友^
谎友^ 2020-11-27 21:34

I have a directory, and inside it are files are named \"a_id_XXX.zip\".

How do check if a file exists given an id and File dir

7条回答
  •  醉梦人生
    2020-11-27 21:52

    i created zip files named with a_id_123.zip ,a_id_124.zip ,a_id_125.zip ,a_id_126.zip and it looks like working fine but i'm not sure if it's proper answer for you. Output will be the following if files listed above exists

    • 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);
                  }
              }
          }
      }
      

提交回复
热议问题