2013-08-06 55 views
0

我會第一時間發佈代碼,然後闡述:無法刪除文件,因爲它是在使用

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仍以某種方式仍在使用中。任何想法如何解鎖文件,以便我可以刪除它?備註:如果驗證成功,則刪除文件夾也會成功。

回答

2

您需要在刪除文件夾之前關閉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; 
    } 
} 

希望工程!祝你好運!

btw。您應始終關閉文件和流以避免內存泄漏,從而導致OutOfMemoryException和ConcurrentModificationException異常。 [/編輯]

+0

我找不到'的getInputStream()'方法。 –

+0

我需要使用'StreamSource'而不是'Source'來獲取該方法。 –

+0

^^ true,我在示例代碼中更正了它。現在在工作嗎? – mondjunge

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; 
    } 
} 
+0

使用'File'作爲'new StreamSource(File ...)'的參數有助於解決我的問題!謝謝 –

相關問題