Java - XStream 关​​闭与文件的连接

发布于 2024-12-16 21:52:55 字数 218 浏览 0 评论 0原文

我像这样加载 XML:

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

成功使用 XStream 后,我想删除我的文件。我无法这样做,因为 XStream 仍处于打开状态,因此无法删除我的文件。如何关闭连接并删除文件?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我鬧 2024-12-23 21:52:55
File file = new File(...);
try (InputStream inputStream = new FileInputStream(file)) {
    ...
    xstream.fromXML(file);
    ...
} catch (Exception e) {
    log.debug(e);
} finally {
    inputStream.close();
}

如果抛出异常,则输入流将被正确关闭。如果一切正常,InputStream 将在finally 块中正确关闭。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文