import java.util.*;
public class File extends java.io.File {
File(java.io.File file,String str){
super(file,str);
}
File(String str1,String str2){
super(str1,str2);
}
File(String str){
super(str);
}
File(java.net.URI uri){
super(uri);
}
public static void deleteDirs(java.io.File dir)throws Exception{
m_root=dir;
m_dirs= new ArrayList();
if(!m_root.isDirectory()){
throw new Exception("Exception:\""+m_root.toString()+"\" is not a director");
}
else{
for(int i=0;i<m_dirs.size();i++){
System.out.println(((File)m_dirs.get(i)).toString());
}
//delete all director
try{
m_dirs.add(m_root);
myDelete();
}
catch(Exception e){
e.printStackTrace();
}
}
}
/**
*
* @param dirPath String a director file path;
* @throws IOException if dirPath is not a director file path
*/
public static void deleteDirs(String dirPath)throws Exception{
m_root=new java.io.File(dirPath);
deleteDirs(m_root);
}
public static void deleteSubDirs(String dirPath)throws Exception{
m_root=new java.io.File(dirPath);
deleteSubDirs(m_root);
}
public static void deleteSubDirs(java.io.File dir)throws Exception{
m_root=dir;
m_dirs= new ArrayList();
//deleteDirs(m_root);
if(!m_root.isDirectory()){
throw new Exception("Exception:\""+m_root.toString()+"\" is not a director");
}
else{
for(int i=0;i<m_dirs.size();i++){
System.out.println(((File)m_dirs.get(i)).toString());
}
try{
myDelete();
}
catch(Exception e){
e.printStackTrace();
}
}
}
/*
* visit all a director and save them in a list
*/
private static void visitAll(java.io.File tempRoot){
//m_dirs.add(tempRoot);
//System.out.println("函数开始处:"+m_dirs.size());
java.io.File[] dirs=tempRoot.listFiles();
if(dirs!=null){
List dirsList=Arrays.asList(dirs);
//while(dirsList!=null){
if(dirsList==null){
try{
tempRoot.delete();
}
catch(Exception e){
e.printStackTrace();
}
}
else{
m_dirs.addAll(dirsList);
//System.out.println(m_dirs.size());
/*
for(int i=0;i<dirs.length;i++){//print dirs
System.out.println("NO."+ ++counter+":"+dirs[i].toString());
}
*/
//System.out.println(m_dirs.size());
for(int i=0;i<dirsList.size();i++){
tempRoot=(java.io.File)dirsList.get(i);
visitAll(tempRoot);
/*
dirs=tempRoot.listFiles();
List dirsList= Arrays.asList(dirs);
*/
//System.out.println(tempRoot.toString());
}
}
//return true;
//}
}
}
/*
* do delete
*/
private static void myDelete()throws Exception{
//m_dirs.add(m_root);
visitAll(m_root);
if(m_dirs!=null){
//System.out.println("删除前:"+m_dirs.size());
//System.out.println(m_dirs.size());
for(int i=m_dirs.size()-1;i>=0;i--){
java.io.File f= (java.io.File)m_dirs.remove(i);
//File f= (File)m_dirs.get(i);
//System.out.println(f.toString());
String fileName=f.toString();
if(!f.delete()){
throw new Exception("Exception: delete file "+ fileName+" false!");
//System.out.println("删除文件/文件夹"+fileName+"失败!");
}
}
}
else{
throw new Exception("Exception: read file list of "+m_root.toString()+"false!");
//System.out.println("ArrayList==null");
}
}
/*
* root director
*/
private static java.io.File m_root;
private static ArrayList m_dirs;
//private static int counter=0;
public static void main(String[] args) {//测试类
File file =new File("H:/小说/小四/天亮说晚安");
try{
File.deleteDirs("H:/小说/小四/天亮说晚安");
//File.deleteDirs(file);
//File.deleteSubDirs("H:/小说/小四/天亮说晚安");
//File.deleteSubDirs(file);
//File f=new File("F:/test.txt");
//f.delete();
}
catch(Exception e){
e.printStackTrace();
}
System.out.println("删除成功~");
}
}
posted on 2007-05-21 02:51
jadmin 阅读(51)
评论(0) 编辑 收藏