首页 > 其他 > 详细

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

时间:2015-11-23 16:50:44      阅读:224      评论:0      收藏:0      [点我收藏+]
 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类批量删除文件中重复的文件比如: 好妹妹(2).mp4, 好妹妹(3).mp4

原文:http://www.cnblogs.com/liuyunyi/p/Filematches_frame.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!