Java 无法删除文件,因为它正在使用中
我将首先发布代码,然后详细说明:Java 无法删除文件,因为它正在使用中,java,file,stream,delete-file,Java,File,Stream,Delete File,我将首先发布代码,然后详细说明: public void createPackage(String uploadedZipLocation) throws BadException, IOException, SAXException { String extractedFolderPath = uploadedZipLocation.split("\\.zip")[0]; File tempFolder = null; Source xml = null; tr
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
仍在使用中。验证失败后,我尝试将源代码设置为null
,但web.xml
仍在使用。你知道如何解锁该文件以便我可以删除它吗?旁注:如果验证成功,则删除文件夹也会成功。您需要在删除文件夹之前关闭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;
}
}
希望它能起作用!祝你好运
[编辑]
顺便说一句,您应该始终关闭文件和流以避免内存泄漏,从而导致OutOfMemoryException和ConcurrentModificationException。
[/edit]删除文件夹之前,需要关闭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;
}
}
希望它能起作用!祝你好运
[编辑]
顺便说一句,您应该始终关闭文件和流以避免内存泄漏,从而导致OutOfMemoryException和ConcurrentModificationException。
[/edit]试试看
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;
}
}
试一试
我找不到getInputStream()
方法。我需要使用StreamSource
而不仅仅是Source
来获得该方法。^^true,我在示例代码中更正了它。正在工作吗?可能我们现在可以直接关闭FileInputStream,而不是在StreamSource上使用getINputStream方法。是的,我刚才看到了编辑。这似乎非常重要。谢谢你的帮助。它现在工作得很好!我找不到getInputStream()
方法。我需要使用StreamSource
而不仅仅是Source
来获得该方法。^^true,我在示例代码中更正了它。正在工作吗?可能我们现在可以直接关闭FileInputStream,而不是在StreamSource上使用getINputStream方法。是的,我刚才看到了编辑。这似乎非常重要。谢谢你的帮助。它现在工作得很好!使用File
作为newstreamsource(File…
的参数有助于解决我的问题!感谢使用文件
作为新StreamSource(文件…
的参数帮助解决了我的问题!谢谢