zoukankan      html  css  js  c++  java
  • 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());
            }
          }
        }

  • 相关阅读:
    PL/SQL developer连接oracle出现“ORA-12154:TNS:could not resolve the connect identifier specified”问题的解决
    POJ 1094-Sorting It All Out(拓扑排序)
    Windows剪贴板操作简单小例
    我的高效编程的秘诀--开发环境的重要性(IOS)
    js操作cookie的一些注意项
    解决 libev.so.4()(64bit) is needed by percona-xtrabackup-2.3.4-1.el6.x86_64案例
    my.cnf 详解
    keepalived的log
    keepalive配置mysql自动故障转移
    说说能量守恒定律
  • 原文地址:https://www.cnblogs.com/secbook/p/2655448.html
Copyright © 2011-2022 走看看