我要删除程序中生成的一些临时文件,这样的代码网上有很多。
我在windows下测试时,有时可以全部删除,有时会有几个文件不能删除。
我在删除文件前,将所有这些使用过的文件都close了,但在linux下,一个文件也删除了,
使用 Runtime.getRuntime().exec("rm -rf "+
也不行。。请高手指点

解决方案 »

  1.   

    public static void delAllFile(String path) {
    File file = new File(path);
    if (!file.exists()) {
    return;
    }
    if (!file.isDirectory()) {
    return;
    }
    String[] tempList = file.list();
    File temp = null;
    for (int i = 0; i < tempList.length; i++) {
    if (path.endsWith(File.separator)) {
    temp = new File(path + tempList[i]);
    } else {
    temp = new File(path + File.separator + tempList[i]);
    }
    if (temp.isFile()) {
    temp.delete();
    }
    if (temp.isDirectory()) {
    delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
    delFolder(path + "/" + tempList[i]);// 再删除空文件夹
    }
    }
    }
      

  2.   


    public static void delAllFile(String path) {
    File file = new File(path);
    if (!file.exists()) {
    return;
    }
    if (!file.isDirectory()) {
    return;
    }
    String[] tempList = file.list();
    File temp = null;
    for (int i = 0; i < tempList.length; i++) {
    if (path.endsWith(File.separator)) {
    temp = new File(path + tempList[i]);
    } else {
    temp = new File(path + File.separator + tempList[i]);
    }
    if (temp.isFile()) {
    temp.delete();
    }
    if (temp.isDirectory()) {
    delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
    delFolder(path + "/" + tempList[i]);// 再删除空文件夹
    }
    }
    }
      

  3.   

    File temp = File.createTempFile("temp","xxxx");
    // do sth with temp
    temp.deleteOnExit();
      

  4.   

    但是,到linux下,直接  rm -rf 目录,却可以删除
      

  5.   

    Linux下删除不掉有可能与权限有关系,看一下你这些文件的权限。
      

  6.   


    看了下你的代码,不明白这几句的用意:
     String[] tempList = file.list();
            File temp = null;
            for (int i = 0; i < tempList.length; i++) {
                if (path.endsWith(File.separator)) {
                    temp = new File(path + tempList[i]);
                } else {
                    temp = new File(path + File.separator + tempList[i]);
                }你为何不用File[] files = dirFile.listFiles();直接列出文件,然后调用方法删除呢,何苦去获取路径再new呢给你一段参考代码,我写的:
    /**
     * Delete all files and folders in the directory. Directory may end with a
     * "\" or not.
     * 
     * @param path
     *            - directory path
     * @return - true if successfully, otherwise false.
     */
    public static boolean deleteFolder(String path) {
    boolean flag = true;
    File dirFile = new File(path);
    if (!dirFile.isDirectory()) {
    return flag;
    }
    File[] files = dirFile.listFiles();
    for (File file : files) {
    // Delete file.
    if (file.isFile()) {
    flag = deleteFile(file);
    } else if (file.isDirectory()) {// Delete folder
    flag = deleteFolder(file.getAbsolutePath());
    }
    if (!flag) {
    break;
    }
    }
    flag = dirFile.delete();
    return flag;
    } /**
     * Delete file.
     * 
     * @param file
     *            - deleted file
     * @return - true if delete successfully, otherwise false.
     */
    public static boolean deleteFile(File file) {
    boolean flag = false;
    if (file.exists() && file.isFile()) {
    file.delete();
    flag = true;
    }
    return flag;
    }
      

  7.   

    File temp = File.createTempFile("temp","xxxxxxxxx",YouSelectedDirectory);
      

  8.   

    linux 下的文件路径分隔符号和windows是不相同的。\好象是反斜杠
      

  9.   

    权限问题,狠一点:chmod 777 /tmp
      

  10.   

    下面是linux下有关权限的命令 楼住可以参考下  / 我自己记得不太清楚了,linux的路径的分割是 /  hehe    文件权限 
            r    读权限。对普通文件来说,是读取该文件的权限;对目录来说,是获得该目录下的文件信息。
            w    写权限。对文件,是修改;对目录,是增删文件与子目录。
                (注 删除没有写权限的文件可以用 rm -f ,这是为了操作方便,是人性化的设计)。
            x    执行权限;对目录,是进入该目录
            -    表示没有权限
           形式 - rw- r-- r--  
        其中 第一个是文件类型(-表普通文件,d表目录,l表软链接文件)
        第2~4个是属主,生成文件时登录的人,权限最高,用u表示
        第5~7个是属组,系统管理员分配的同组的一个或几个人,用g表示
        第8~10个是其他人,除属组外的人,用o表示
        所有人,包括属主、属组及其他人,用a表示chmod  更改权限;
        用法    chmod [-fR] <绝对模式> 文件 ...
               chmod [-fR] <符号模式列表> 文件 ...
          其中    <符号模式列表> 是一个用逗号分隔的表     [ugoa]{+|-|=}[rwxXlstugo]
        chmod u+rw  给用户加权限。同理,u-rw也可以减权限。
        chmod u=rw  给用户赋权限。与加权限不一样,赋权限有覆盖的效果。
        主要形式有如下几种 
        chmod u+rw       chmod u=rw
        chmod u+r, u+w   chmod u+rw,g+w, o+r
        chmod 777( 用数字的方式设置权限是最常用的)
        数字表示权限时,各数位分别表示属主、属组及其他人;
          其中,1是执行权(Execute),2是写权限(Write),4是读权限(Read),
          具体权限相当于三种权限的数相加,如7=1+2+4,即拥有读写和执行权。
        另外,临时文件/目录的权限为rwt,可写却不可删,关机后自动删除;建临时目录:chmod 777 目录名,再chmod +t 目录名。
      

  11.   


    那如果来解决呢。。目录的权限是:drwxrwxr-x
    下面文件的权限是:-rw-rw-r--
      

  12.   

    用deleteOnExit(),能删除部分文件 ,但是还是不能保证完全删除。请高手指点一下,,
      

  13.   


    运行完了,在linux下可以正常删除
      

  14.   


    权限都有。。我现在估计是因为java的回收机制导致,
    我虽然关闭了文件流,
    但是JVM还要过段时间才能释放,
    所以,导致有些文件不能删除,
    而程序执行完,却能删除。
      

  15.   

             if (temp.isDirectory()) {
                    delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
                    delFolder(path + "/" + tempList[i]);// 再删除空文件夹
                }
    应该是"/"的问题。
      

  16.   

    Stirng fileseparator = System.getProperty("file.separator");
    用fileseparator 替换"/"试试
      

  17.   

    public void cleanUpDir(String path){
        File workDir = new File(path);
        File[] children = workDir.listFiles();    
        for (File child : children) {
    if(child.isDirectory()){
        deleteDir(child); // call another method
    }else{
                child.delete();
    }
        }
    }private boolean deleteDir(File dir){
        try{
            if (dir.exists() && dir.isDirectory()){
        File[] children = dir.listFiles();
        for (int i = 0; i < children.length; i++) {
    if (children[i].isDirectory()) {
        deleteDir(children[i]); //call the method itself
    } else {
        children[i].delete();
    }
        }
    } return dir.delete();
        } catch (Exception e) {
    e.printStackTrace();
        }
    }
      

  18.   

    File对象有删除方法,不管是目录和文件都可以