如何判断子目录下,是否有已知文件存在?急 有没有通过封装好的方法,直接送入文件名,就可以判断出该文件是否存在呢?因为\upload\data\下的目录会很多,根据日期生成的! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼主自己修改一下吧:/* * 删除一个目录下的所有文件 */ public static void delAllFile(String path) { File file = new File(path); if(!file.exists()) return; if(!file.isDirectory()) return; String[] tempList = file.list(); File temp = null; for(int i = 0; i < tempList.length; i++) { if(path.endsWith(File.separator)) temp = new File(path + tempList[i]); else temp = new File(path + File.separator + tempList[i]); if(temp.isFile()) temp.delete(); if(temp.isDirectory()) delAllFile(path + tempList[i]); }} 下面方法返回查找文件的路径,改变一下返回值就可以得到你的效果1static public String Findfile(String dir, String to_find) { int i; String results; String dir_list[] = (new File(dir)).list(); for(i = 0; i < dir_list.length; i ++ ) { File to_test = new File(dir, dir_list[i]); if(to_test.isDirectory()) { results = Findfile(to_test.getAbsolutePath(), to_find); if(results.length() > 0) return results; } else { if((to_test.getName()).equalsIgnoreCase(to_find)) return to_test.getAbsolutePath(); } } return ""; } 只好递归搜索所有目录了。判断文件是否存在可以在创建文件对象,然后调用exsits 可以使用这样的方法class FileFinder implements FileFilter { String fileName; public FileFinder(String fileName) { this.fileName = fileName; } public boolean accept(File file) { if (file.isDirectory()) { URL fileURL = new URL(file.toURL(), fileName); File newFile = new File(fileURL.getPath());//jdk1.5例可以直接用fileURL.toURI(); return newFile.exists(); } return false; }}用的时候 File[] files = new File("c:\\upload\\data\\").listFiles(new FileFinder);if (files.length>0) { //存在}这样FileFinder就以Strategy模式作为可替换的组件了 如果需要查找多层class FileFinder implements FileFilter { String fileName; public FileFinder(String fileName) { this.fileName = fileName; } public boolean accept(File file) { if (file.isDirectory()) { URL fileURL = new URL(file.toURL(), fileName); File newFile = new File(fileURL.getPath());//jdk1.5例可以直接用fileURL.toURI(); boolean accepted = newFile.exists(); if (accepted) return true; return newFile.listFiles(new FileFinder(fileName)); } return false; }} 上面错了一行return newFile.listFiles(new FileFinder(fileName));=〉return newFile.listFiles(new FileFinder(fileName)).length >0; 关于锁的问题。。。 HashMap的问题 求解程序出错原因 Eclipse和SWT Designer的安装 如何构造这棵树呢 关于sqlserver2k连接jbuilder9的问题,急~~~~,请各位大虾帮忙解决呀~~~~谢谢了~~~ 产生doc后为什么有乱码啊? 用java如何实现gb2312和utf-8之间的转换 请指教 要运行JDBC的程序,该如何设置? 接触java不久,问个问题!希望大家帮助. VB能不能调用java程序?如何调用?请教各位高手!急!!!!
* 删除一个目录下的所有文件
*/
public static void delAllFile(String path) {
File file = new File(path);
if(!file.exists()) return;
if(!file.isDirectory()) return;
String[] tempList = file.list();
File temp = null;
for(int i = 0; i < tempList.length; i++) {
if(path.endsWith(File.separator))
temp = new File(path + tempList[i]);
else
temp = new File(path + File.separator + tempList[i]);
if(temp.isFile()) temp.delete();
if(temp.isDirectory()) delAllFile(path + tempList[i]);
}
}
static public String Findfile(String dir, String to_find)
{
int i;
String results;
String dir_list[] = (new File(dir)).list();
for(i = 0; i < dir_list.length; i ++ )
{
File to_test = new File(dir, dir_list[i]);
if(to_test.isDirectory())
{
results = Findfile(to_test.getAbsolutePath(), to_find);
if(results.length() > 0)
return results;
}
else
{
if((to_test.getName()).equalsIgnoreCase(to_find))
return to_test.getAbsolutePath();
}
}
return "";
}
String fileName;
public FileFinder(String fileName) {
this.fileName = fileName;
}
public boolean accept(File file) {
if (file.isDirectory()) {
URL fileURL = new URL(file.toURL(), fileName);
File newFile = new File(fileURL.getPath());//jdk1.5例可以直接用fileURL.toURI();
return newFile.exists();
}
return false;
}
}用的时候 File[] files = new File("c:\\upload\\data\\").listFiles(new FileFinder);
if (files.length>0) {
//存在
}这样FileFinder就以Strategy模式作为可替换的组件了
class FileFinder implements FileFilter {
String fileName;
public FileFinder(String fileName) {
this.fileName = fileName;
}
public boolean accept(File file) {
if (file.isDirectory()) {
URL fileURL = new URL(file.toURL(), fileName);
File newFile = new File(fileURL.getPath());//jdk1.5例可以直接用fileURL.toURI();
boolean accepted = newFile.exists();
if (accepted) return true;
return newFile.listFiles(new FileFinder(fileName));
}
return false;
}
}
return newFile.listFiles(new FileFinder(fileName));
=〉
return newFile.listFiles(new FileFinder(fileName)).length >0;