3

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?

E. van der Spoel
  • 260
  • 1
  • 15
  • Which StreamReader have you configured for XStream? – rit Nov 17 '11 at 20:24
  • I guess that you have add more details. The snapshot that you provided is not helping to identify what the issue is. After fromXml is executed the file stream is closed. What OS are you using? – devsprint Nov 17 '11 at 20:03
  • 1
    I found the answer here: http://stackoverflow.com/questions/991489/i-cant-delete-a-file-in-java System.gc(); did the trick, their wasn't a lot of things I did after this. I am running XP on dev, Ubuntu server on live – E. van der Spoel Nov 17 '11 at 20:42
  • So then it is solved? But how is this related to GC? – rit Nov 17 '11 at 20:54
  • 1
    Yhe its solved but not in a nice way. I believe fromXML(File f) does not close the connection nicely. – E. van der Spoel Nov 17 '11 at 20:59

1 Answers1

2
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.

Dirk Rother
  • 440
  • 1
  • 4
  • 18