答案:/**
* @author www.nwu.edu.cn MiniRice
* @author darkrice@gmail.com
* @version 1.2
*
*/
package cn.edu.nwu.io;
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++){
 
上一个:实现Java与C语言接口
下一个:学习Java的笔记(4)