日期:2014-05-20  浏览次数:20776 次

在线等,java中如何判断某一目录下某类文件是否存在?
请问各位,如果我想判断在某一目录下是否存在*.txt文件,要怎么做?先再次谢过。

------解决方案--------------------
public void testDir(String path)
{
File f = new File(path);
if (f.exists() && f.isDirectory())
{
if (f.listFiles().length > 0)
{

String[] file1 = f.list(new FilenameFilter(){//使用匿名内部类重写accept方法
public boolean accept(File dir, String name){
if(new File(dir,name).isDirectory()){
return true;
}
return name.indexOf( ".txt ")!=-1;//筛选出.txt和.wav文件
}

}
);
for (int j = 0; j < file1.length; j++)
{
if(new File(path+ "/ "+file1[j]).isFile())
System.out.println(file1[j]);
}
}
}
}
------解决方案--------------------
package test1;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test10 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
File f=new File( "d:/ ");
String[] str=f.list();
Pattern p=Pattern.compile( "(.+\\.txt) ");
for(int i=0;i <str.length;i++){
Matcher m=p.matcher(str[i]);
if(m.find())
System.out.print(m.group());
}
}

}