如何在程序中实现检索一个文件夹下面的文件?急需求助

yufengxilovewho 2003-10-13 12:41:15
例如:
在检索状态下打上aaa-*-ddd.txt

检索过后找到如下的文件.
aaa-www-ddd.txt
aaa-eee-ddd.txt
aaa-qqq-ddd.txt
aaa-rrr-ddd.txt

请问该如何实现呢?
...全文
23 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
yufengxilovewho 2003-10-15
  • 打赏
  • 举报
回复
我已经做出来了,谢谢了,虽然说你的方法挺繁锁的,但是还是很感谢你了.

分全都给你了,呵呵
tianmiaohu 2003-10-13
  • 打赏
  • 举报
回复
Here is the code:

import java.io.*;
import java.util.regex.*;
public class DirFilter{
public static void main(String args[])
{
// No error checking anywhere in this code, place your own
String filterArg = args[0];
// Replace * with .* as requtested in Regular Expression
// Replace . with \. as requtested in Regular Expression
Pattern pn = Pattern.compile("\\.");
filterArg = pn.matcher(filterArg).replaceAll("\\\\.");
pn = Pattern.compile("\\*");
filterArg = pn.matcher(filterArg).replaceAll("\\.\\*");
// System.out.println(filterArg);
// Search against the current directory (Unix way of directory for compatibility)
File theDir = new File("./.");
String fileList[] = theDir.list();
pn = Pattern.compile(filterArg, Pattern.CASE_INSENSITIVE);
Matcher mt = pn.matcher(fileList[0]);
if(mt.find())
{
System.out.println(fileList[0]);
}
for(int i = 1; i<fileList.length; i++)
{
System.out.println(fileList[i]);
mt = mt.reset(fileList[i]);
if(mt.find())
{
System.out.println(fileList[i]);
}
}
}
}

You have to compile and run it under JDK 1.4 or later as it utilizes Regular Expression.

Not optimized. No error checking. But it compiles and works. Check it out.

81,091

社区成员

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

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