java搜索文件小程序(递归搜索文件)

起因:搜索电脑中一个视频文件,但不记得文件名了,windows搜索后缀名没搜到,所以想着打印出大于50M的所有文件

public class SearchFile {

  
 private static void search(File file){
       if(file.isDirectory()){//如果是目录
           File[] files = file.listFiles();
           for(File f : files){//遍历目录下所有文件
            try {
               search(f);
            }catch(Exception e) {
            System.out.println("文件路径" + f.getAbsolutePath() + f.getName());
            e.printStackTrace();
            }
           }
       
       }else if(file.length() >= (1024*50*1024) && !file.getName().endsWith(".pdf") && !file.getName().endsWith(".zip")
        && !file.getName().endsWith(".azw3")  && !file.getName().endsWith(".PDF")   && !file.getName().endsWith(".rar") 
        &&!file.getName().endsWith(".caj")&&!file.getName().endsWith(".epub")&&!file.getName().endsWith(".jpg")
        &&!file.getName().endsWith(".exe")
        &&!file.getName().endsWith(".mobi")){
          System.out.println(file.getAbsolutePath() +"||" + file.getName());
           } 
            
     }
   
   public static void main(String[] args) {
    try {
       search(new File("g:\\"));
    }catch(Exception e) {
    e.printStackTrace();
    }
   }
}

猜你喜欢

转载自blog.csdn.net/ccityzh/article/details/79951189
今日推荐