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()匹配的方法;
这是一个小程序,希望与大家分享
总结
以上就讲解了一下文件匹配的简单使用,虽然只是一个辅助的功能,
请支持原创,尊重原创,转载请注明出处。谢谢。
新闻热点
疑难解答