posts - 495,  comments - 11,  trackbacks - 0

          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 閱讀(54) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 九龙坡区| 琼中| 高阳县| 深圳市| 涞水县| 铜鼓县| 金川县| 响水县| 安化县| 白朗县| 永仁县| 吴堡县| 麻栗坡县| 乌鲁木齐县| 尖扎县| 普定县| 桂东县| 尼玛县| 凤冈县| 新津县| 武义县| 哈巴河县| 香河县| 彭水| 莱州市| 平谷区| 盐津县| 苏尼特右旗| 奉新县| 镇沅| 安仁县| 沿河| 阳高县| 沾化县| 绥江县| 印江| 嘉善县| 绿春县| 天祝| 麻江县| 大理市|