用File类批量删除文件中重复的文件比如: 好妹妹(2).mp4, 好妹妹(3).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()匹配的方法;

 

 

 

 

 

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

总结

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

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

 

转载于:https://www.cnblogs.com/liuyunyi/p/Filematches_frame.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值