我使用api JavaAPIforKML 2.2版生成了一个kml文件:
try {
File kmlFile =files.get(TypeOfFile.kml);
kmlMaker.marshal(kmlFile);
logger.debug("The file " + kmlFile.getPath() + " has been created");
} catch (FileNotFoundException e) {
logger.error(e.getMessage());
throw new CoreServiceException("Error while the creation of KML file", e);
}
生成之后,我尝试使用此代码删除kml文件,但它不起作用:
for(int i=0;i<directoryFiles.length;i++){
String filePath = directoryFiles[i].getPath();
boolean fileIsDeleted = false;
try {
FileDeleteStrategy.FORCE.delete(directoryFiles[i]);
fileIsDeleted = true;
} catch (IOException e) {
e.printStackTrace();
}
if(fileIsDeleted) logger.debug("The file " + filePath + " has been deleted");
else logger.error("The file " + filePath + " hasn't been deleted");
}
抛出此异常:java.io.IOException:无法删除文件:C:\ DEV \ Tmp \ KML \ 3013899924069204776 \ 1662188016.kml
你有什么建议吗?我查看了api的源代码,但是我没有所有的源代码,我认为outputStream没有在api中关闭。
您将在下面找到关于此问题的小JUnit,您可以注意到删除该文件不起作用:
@Test
public void deleteKML() throws Exception {
Kml kmlMaker = new Kml();
File kmlFile = new File("C:\\Dev\\Tmp\\KML\\TEST1-913289299.kml");
kmlMaker.marshal(kmlFile);
Assert.assertTrue(kmlFile.delete());
}
提前感谢您的建议和帮助。