/**
* 检索文件夹
* @param filepath
* @return
* @throws IOException
*/
@Override
public boolean readfile(String filepath) throws IOException {
try {
File file = new File(filepath);
if (!file.isDirectory()) {
// 你想做的操作
} else if (file.isDirectory()) {
System.out.println("文件夹");
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "\\" + filelist[i]);
if (!readfile.isDirectory()) {
// 你想做的操作
} else if (readfile.isDirectory()) {
readfile(filepath + "\\" + filelist[i]);
}
}
}
} catch (Exception e) {
System.out.println("readfile() Exception:" + e.getMessage());
e.printStackTrace();
}
return true;
}
/**
* 删除某个文件夹下的所有文件夹和文件
* @param delpath
* @return
* @throws IOException
*/
public boolean deletefile(String delpath) throws IOException {
try {
File file = new File(delpath);
if (!file.isDirectory()) {//这不是目录
file.delete();
} else if (file.isDirectory()) {
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File delfile = new File(delpath + "\\" + filelist[i]);
if (!delfile.isDirectory())
delfile.delete();
else if (delfile.isDirectory())
deletefile(delpath + "\\" + filelist[i]);
}
file.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
检索或清空文件夹
最新推荐文章于 2024-08-16 15:33:24 发布