範例:
package com.prc.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class DelDirTest {
private static String dirPath = "E:/fileTest";
private static String[] delDirName = { ".svn" };
/**
* @param args
*/
public static void main(String[] args) {
try {
List<File> fileList = new ArrayList<File>();
visitedDirectory(dirPath, fileList);
//列出資料夾裡的檔案及資訊
for (File file : fileList) {
System.out.println(file.getName() + " " + new FileInputStream(file).available());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void visitedDirectory(String path, List<File> fileList) {
File file = new File(path);
File[] subFile = file.listFiles();
for (int i = 0; i < subFile.length; i++) {
if (subFile[i].isDirectory()) {
if (delDirName[0].equals(subFile[i].getName())) {
deleteDirectory(subFile[i]);
} else {
visitedDirectory(subFile[i].getAbsolutePath(), fileList);
}
} else {
fileList.add(subFile[i]);
}
}
}
public static boolean deleteDirectory(File directory) {
if (!directory.exists() || !directory.isDirectory()) {
return false;
}
String[] files = directory.list();
for (int i = 0, len = files.length; i < len; i++) {
File f = new File(directory, files[i]);
if (f.isDirectory()) {
deleteDirectory(f);
} else {
f.delete();
}
}
return directory.delete();
}
}
沒有留言:
張貼留言