J2ME FileConnection 删除整个目录

    最近一个项目里用到了JSR75-FileConnection,第一次用某个API总会出不少问题, 
这里记录下来,同时也和大家分享一下。

    本文问题:删除目录的时候是用FileConnection的delete()函数,发现对目录只有空的才能删除,
否则会报java.io.IOException。没办法,只能自己写一个删除函数了,目录、文件通统杀掉。
void delete() Deletes the file or directory specified in the Connector.open() URL.

/*
* function: 删除文件或者目录,如果是目录,则删除目录下所有子目录和文件.
* param: String name -- 要删除的文件或者目录全路径,比如: "file:///root1/testdir/"
* auther: smilelance
*/

    private void deleteDirFiles(String name){
      print("to be deleted: " + name);
      FileConnection fc = null;
      try {
        fc=(FileConnection)Connector.open(name);
        if(fc.exists()){
          if(!fc.canWrite()){
            print("file is read only.");
            fc.setReadable(true);
//            if(!fc.canWrite()){
//              print("read only after set");
//            }
          }
          if(fc.isDirectory()){
            Enumeration e = fc.list();
            while(e.hasMoreElements()){
              //System.out.println(e.nextElement());
              deleteDirFiles(name + e.nextElement());
            }
          }
          fc.delete();
        }else{
          print("file don't exsit!");
        }
     }catch (Exception   e) {
        System.out.println( "Error: "   +   e.toString());
      } finally   {
        try {
          fc.close();
        } catch(Exception   e) {
          System.out.println(e.toString());
        }
      }
    }

原文地址:https://www.cnblogs.com/secbook/p/2655448.html