java文件操作工具类(三)
h();
output.close();
input.close();
}
if (file[i].isDirectory()) {
log.debug("拷贝:" + file[i].getAbsolutePath() + "-->"
+ desDirector + "/" + file[i].getName());
copyFileWithDirector(srcDirector + "/" + file[i].getName(),
desDirector + "/" + file[i].getName());
}
}
}
/**
*
* 删除文件夹
*
* @param folderPath
* folderPath 文件夹完整绝对路径
*/
public static void delFolder(String folderPath) throws Exception {
// 删除完里面所有内容
delAllFile(folderPath);
String filePath = folderPath;
filePath = filePath.toString();
File myFilePath = new File(filePath);
// 删除空文件夹
myFilePath.delete();
}
/**
*
* 删除指定文件夹下所有文件
*
* @param path
* 文件夹完整绝对路径
*/
public static boolean delAllFile(String path) throws Exception {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return flag;
}
if (!file.isDirectory()) {
return flag;
}
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]);
// 再删除空文件夹
delFolder(path + "/" + tempList[i]);
flag = true;
}
}
return flag;
}
/**
* 功能描述:列出某文件夹及其子文件夹下面的文件,并可根据扩展名过滤
*
* @param path
* 文件夹
*/
public static void list(File path) {
if (!path.exists()) {
System.out.println("文件名称不存在!");
} else {
if (path.isFile()) {
if (path.getName().toLowerCase().endsWith(".pdf")
|| path.getName().toLowerCase().endsWith(".doc")
|| path.getName().toLowerCase().endsWith(".chm")
|| path.getName().toLowerCase().endsWith(".
html")
|| path.getName().toLowerCase().endsWith(".htm")) {// 文件格式
System.out.println(path);
System.out.println(path.getName());
}
} else {
File[] files = path.listFiles();
for (int i = 0; i < files.length; i++) {
list(files[i]);
}
}
}
}
/**
* 功能描述:拷贝一个目录或者文件到指定路径下,即把源文件拷贝到目标文件路径下
*
* @param source
* 源文件
* @param target
* 目标文件路径
* @return void
*/
public static void copy(File source, File target) {
File tarpath = new File(target, source.getName());
if (source.isDirectory()) {
tarpath.mkdir();
File[] dir = source.listFiles();
for (int i = 0; i < dir.length; i++) {
copy(dir[i], tarpath);
}
} else {