我从jsp页面form中提交信息,创建文件放到一个文件夹下,将文件夹里创建的文件列在页面上,通过选中文件来删除,但是删除不成功。创建文件  public boolean createLicense(String filename) throws LogicException
    {
        ESMEncrypt encrypt = new ESMEncrypt();
        String strInput = "the content of this file";
        // 判断当前操作系统类型
        File parentFile = null;
        File file = null;
        try
        {
            parentFile=new File(getClass().getResource("/myfile").toURI());
            if(parentFile.isDirectory())
            {
                File child=new File(parentFile,filename);
                if(!child.exists())
                {
                    child.mkdir();
                }
                String osType = child.getPath() + "\\file.sss";
                if (osType != null)
                {
                    file = new File(osType);
                }
                // 如果已经有
                if (file.exists())
                {
                    file.delete();
                }
                if (!file.exists())
                {
                    file.createNewFile();
                }
                FileOutputStream outs = new FileOutputStream(file);
                outs.write(encrypt.encrypt(strInput.getBytes(), Cipher.ENCRYPT_MODE));
                outs.close();
               }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
        return true;
    }
删除代码   public static boolean delAllFile(String path)
    {
        boolean flag=false;
        File file=new File(path);
        if(file.exists())
        {
            if(file.isDirectory())
            {
                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();
                    }
                  }
            }
        }
        return flag;
    }
    public static boolean delFolders(String folderPath)
    {
        boolean flag=false;
        try
        {
            if(delAllFile(folderPath))
            {
                String filePath=folderPath;
                File myFilePath=new File(filePath);
                if(myFilePath.delete())
                {
                    flag=true;
                }
            }
        }
        catch (Exception e)
        {
           flag=false;
           e.printStackTrace();
        }
        return flag;
    }

解决方案 »

  1.   

    delete 不掉可能是文件在被使用 ,很有可能是 打开了 没有close, 或者在 close之前 试图去delete
      

  2.   

    你要close文件,这样Java才会释放资源(不等于释放内存)
      

  3.   

    问题解决了
    文件流什么的都关闭了
    问题可能出在获取文件列表的时候,使用的是File没有释放掉,
    将其用path字符串生成后 再释放掉,目前只能这样解释了。谢谢各位!