简体   繁体   中英

Java - XStream closing connection to File

I load my XML like this:

File f = new File("Results\\" + filename);
xstream.fromXML(f);
Boolean delete = f.delete();

After using XStream successfully I want to delete my file. I am not able to do so because XStream is still open and so my file can't be deleted. How can I close my connection and delete my file?

File file = new File(...);
try (InputStream inputStream = new FileInputStream(file)) {
    ...
    xstream.fromXML(file);
    ...
} catch (Exception e) {
    log.debug(e);
} finally {
    inputStream.close();
}

If an exception was thrown, the inpuStream would be closed correctly. And if everthing works fine - the InputStream would be closed corretly within the finally block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM