星空网 > 软件开发 > Java

用File类批量删除文件中重复的文件比如: 好妹妹.mp4, 好妹妹.mp4

 1 public class HH { 2   public static void main(String[] args) { 3     String s="he is a boy"; 4     int result=s.indexOf("boy");

        我用的是indexOf()方法;
       5 if(result>=0){ 6 System.out.println("boy是he is a boy的一部分"); 7 }else{ 8 System.out.println("boy不是he is a boy的一部分"); 9 }10 }11 }

其实这是在网上找的方法,我在想是不是可以用字符匹配可是我对matches()还并不太会用,所以我想到了一个好的方法;

根据是上述代码!

File file = new File("d:/");

定义了文件的路径;

 1 File file = new File("E:/迅雷下载/videos"); 2      3   File [] file2 = file.listFiles(new FileFilter() { 4     //这里返回的是一个File的数组 5     @Override 6     public boolean accept(File path) { 7       int result = path.getName().indexOf("(5)");
                         //这里的IndexOf()获取(5)的位置并返回它的位置如果有肯定大于0
           8 int result2 = path.getName().indexOf("(6)"); 9 int result3 = path.getName().indexOf("(7)");10 if (result>0||result2>0||result3>0) {11 return true;12 }13 return false;14 }
15 });
     for (File file3 : file2) {      file3.delete();      //System.out.println(file3.getName());    }

 


这是我的方法这样写,非常的麻烦。

后来我问了一下我的导师,接下类是他的方法;

 

 1 File [] files = folder.listFiles(new FileFilter() { 2        3       @Override 4       public boolean accept(File path) { 5         if (path.getName().matches(".*\\([1-9]\\).*")){ 


                              \\( == ( 这里使用的是转义字符


6 return true; 7 } 8 return false; 9 }10 });

这是使用的String.matches()匹配的方法;

 

 

 

 

 

这是一个小程序,希望与大家分享

总结

  以上就讲解了一下文件匹配的简单使用,虽然只是一个辅助的功能,

  请支持原创,尊重原创,转载请注明出处。谢谢。

 




原标题:用File类批量删除文件中重复的文件比如: 好妹妹.mp4, 好妹妹.mp4

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

E-Bike品牌EMotorad获战略融资!:https://www.kjdsnews.com/a/1840707.html
Shopee订单量激增5倍多,越南成韩国美容品牌主要消费市场:https://www.kjdsnews.com/a/1840708.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流