• 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());
            }
          }
        }

  • 相关阅读:
    java多线程为什么要用while而不是if
    爆竹声响,新人入场
    java多线程状态转换
    java接口的方法默认都是public abstract类型
    java中的静态static关键字
    我的第一个python爬虫程序
    python爬取某些网站出错的解决办法
    spring
    hibernate中文乱码问题
    eclipse(Version: Neon Release (4.6.0))安装hibernate tools
  • 原文地址:https://www.cnblogs.com/secbook/p/2655448.html
Copyright © 2020-2023  润新知