如何实现文件搜索?

datal 2003-12-10 03:49:58
想要通过通配符来查找指定目录下的文件名,用什么类好?最好给个例子!
...全文
18 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
datal 2003-12-10
  • 打赏
  • 举报
回复
楼上两位,你们的代码应该都不能支持通配符吧?比如“*”或者“?” ???
wendi 2003-12-10
  • 打赏
  • 举报
回复
这是一个简单的文件过滤器
public class test {
public static void main(String[] args) {
File bb = new File("指定的目录");
File[] files = bb.listFiles(new FileFilter(){
public boolean accept(File pathname) {
int index = pathname.getName().lastIndexOf(".");
if ("java".equals(pathname.getName().substring(index + 1))) {
return true;
}
return false;}});
}
}
pathname是要过滤的文件,也可以是一个字符串。
grilmm 2003-12-10
  • 打赏
  • 举报
回复
//: DirList3.java
// Building the anonymous inner class "in-place"
import java.io.*;

public class DirList3 {
public static void main(final String[] args) {
try {
File path = new File(".");
String[] list;
if(args.length == 0)
list = path.list();
else
list = path.list(
new FilenameFilter() {
public boolean
accept(File dir, String n) {
String f = new File(n).getName();
return f.indexOf(args[0]) != -1;
}
});
for(int i = 0; i < list.length; i++)
System.out.println(list[i]);
} catch(Exception e) {
e.printStackTrace();
}
}
} ///:~
Yanbin_Q 2003-12-10
  • 打赏
  • 举报
回复
就File呀。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧