【问题标题】:Cannot delete file because it is in use无法删除文件,因为它正在使用中
【发布时间】:2013-08-06 15:39:14
【问题描述】:

我先贴出代码,再详细说明:

public void createPackage(String uploadedZipLocation) throws BadException, IOException, SAXException {
    String extractedFolderPath = uploadedZipLocation.split("\\.zip")[0];
    File tempFolder = null;
    Source xml = null;
    try {
        xml = new StreamSource(extractedFolderPath + "/web.xml");
        validatePkgXml(xml, extractedFolderPath + "/web.xml");
        xml = null; 
    } catch (IOException e) {
        throw e;
    } catch (BadException bpe) {
        xml = null;
        tempFolder = new File(extractedFolderPath);
        FileUtils.deleteDirectory(tempFolder); // **** Can't delete the folder because it is in use.  web.xml is still being read in validatePkgXml I think
        throw bpe;
    }
}


private void validatePkgXml(Source xmlStream, String xmlPath) throws BadException, IOException, SAXException{
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = schemaFactory.newSchema(new File("/C:/workspacesFresh2/web.xsd"));
    Validator validator = schema.newValidator();
    validator.setErrorHandler(new PackageXMLValidationErrorHandler());
    try {
      validator.validate(xmlStream);
      logger.info(xmlStream.getSystemId() + " is valid");
    } catch (SAXException e) {
      logger.error(xmlStream.getSystemId() + " is NOT valid");
      throw new BadException(xmlPath, e.getLocalizedMessage());
    }
}

我正在尝试获取一个 xml 文件并根据 xsd 架构对其进行验证。如果验证失败,我想删除包含 xml 文件的文件夹。当验证失败时,我无法删除该文件夹,因为 web.xml 仍在使用中。在验证失败后,我尝试将Source 设置为null,但web.xml 不知何故仍在使用中。任何想法如何解锁文件以便我可以删除它?旁注:如果验证成功,则删除文件夹也成功。

【问题讨论】:

    标签: java file stream delete-file


    【解决方案1】:

    您需要在删除文件夹之前关闭 InputStream。

    我没有测试过这段代码,但它应该给你的想法:

    public void createPackage(String uploadedZipLocation) throws BadException, IOException, SAXException {
        String extractedFolderPath = uploadedZipLocation.split("\\.zip")[0];
        File tempFolder = null;
        StreamSource xml = null;
        FileInputStream file = null;
    
        try {
            file = new FileInputStream(extractedFolderPath + "/web.xml");
            xml = new StreamSource(file);
            validatePkgXml(xml, extractedFolderPath + "/web.xml");
            xml.getInputStream().close(); 
            //xml = null; 
        } catch (IOException e) {
            xml.getInputStream().close(); 
            throw e;
        } catch (BadException bpe) {
            //xml = null;
            xml.getInputStream().close(); 
            tempFolder = new File(extractedFolderPath);
            FileUtils.deleteDirectory(tempFolder); // **** Can't delete the folder because it is in use.  web.xml is still being read in validatePkgXml I think
            throw bpe;
        }
    }
    

    希望它有效!祝你好运!

    [编辑] 顺便提一句。您应该始终关闭文件和流以避免内存泄漏,从而导致 OutOfMemoryExceptions 和 ConcurrentModificationExceptions。 [/编辑]

    【讨论】:

    • 我找不到getInputStream() 方法。
    • 我需要使用StreamSource 而不仅仅是Source 来获取该方法。
    • ^^ 是的,我在示例代码中更正了它。现在上班了吗?
    • 很可能我们现在可以直接关闭 FileInputStream,而不是使用 StreamSource 上的 getINputStream 方法。
    • 是的,我刚刚看到了那个编辑。这似乎非常重要。谢谢你的帮助。现在完美运行!
    【解决方案2】:

    试试

    public void createPackage(String uploadedZipLocation) throws BadException, IOException, SAXException {
        String extractedFolderPath = uploadedZipLocation.split("\\.zip")[0];
        File tempFolder = null;
        FileInputStream file = null;
        Source xml = null;
        try {
            file = new FileInputStream(extractedFolderPath + "/web.xml");
            xml = new StreamSource(file);
            validatePkgXml(xml, extractedFolderPath + "/web.xml");
            xml = null; 
        } catch (IOException e) {
            throw e;
        } catch (BadException bpe) {
            file.close();
            tempFolder = new File(extractedFolderPath);
            FileUtils.deleteDirectory(tempFolder);
            throw bpe;
        }
    }
    

    【讨论】:

    • 使用File 作为new StreamSource(File ...) 的参数有助于解决我的问题!谢谢
    猜你喜欢
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    • 2016-09-21
    • 2014-05-26
    • 2011-11-20
    • 2017-04-28
    • 1970-01-01
    相关资源
    最近更新 更多