/**
* @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
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
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
system.out.println(“no.”+ ++counter+”:”+dirs[i].tostring());
}
*/
//system.out.println(m_dirs.size());
for(int i=0;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(“j:/毕业设计/测试类/测试文件”);
try{
file.deletedirs(“j:/毕业设计/测试类/测试文件”);
//file.deletedirs(file);
//file.deletesubdirs(“j:/毕业设计/测试类/测试文件”);
//file.deletesubdirs(file);
//file f=new file(“f:/test.txt”);
//f.delete();
}
catch(exception e){
e.printstacktrace();
}
system.out.println(“删除成功~”);
}
}//:~
完毕,问题倒是不难,就是第一尝试用继承和异常。