刪除文件我先發布我的代碼:不能在嘗試捕捉
private void validateXml(String xml) throws BadSyntaxException{
File xmlFile = new File(xml);
try {
JaxbCommon.unmarshalFile(xml, Gen.class);
} catch (JAXBException jxe) {
logger.error("JAXBException loading " + xml);
String xmlPath = xmlFile.getAbsolutePath();
System.out.println(xmlFile.delete()); // prints false, meaning cannot be deleted
xmlFile.delete();
throw new BadSyntaxException(xmlPath + "/package.xml");
} catch (FileNotFoundException fne) {
logger.error("FileNotFoundException loading " + xml + " not found");
fne.printStackTrace();
}
}
你可以在我的評論看到我打印的文件不能被刪除。文件無法從try
/catch
中刪除?所以,如果有一個xml語法錯誤的文件,我想刪除catch
中的文件。
編輯:我可以刪除該文件,當我從此功能外使用delete()
。我在Windows上。
您正在使用哪種操作系統? Windows有鎖定文件的傾向,在Linux/Unix上你可能遇到權限問題。此外,它可能意味着該文件不存在。你可以使用'.exists()'來檢查嗎? –
我現在在Windows上。 –
只是好奇,什麼是抓住'JAXBException'堆棧跟蹤?也許這有助於確定文件是否仍然打開並且在您嘗試刪除文件時被鎖定。 – dic19