“文件过早结束” Java读取和写入XML数据文件时出错 [英] "Premature end of file" error when Java read and writes XML data files

查看:1422
本文介绍了“文件过早结束” Java读取和写入XML数据文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用JDOM库通过Java Servlets读写XML文件。

I have been using JDOM library to read and write XML files through Java Servlets.

问题是,当我使用AJAX向我的servlet发送许多请求时,这些请求会读取和写入XML文件中的数据,很多时候它会显示错误:文件过早结束。如何将所有这些读/写同步到文件中,还是有其他方法可以防止这些问题?如果需要更多详细信息,请与我们联系。

Problem is that when I send many requests using AJAX to my servlets which read and write data in XML files, many times it fails displaying error: Premature end of file. How can I synchronize all these read/writes to files or is there any other way to prevent these problem? Let me know if more details are required.

在这种情况下,使用线程会有什么好处吗?

Will use of threads do any good in this situation?

非常感谢!

推荐答案

两种可能的解决方案是:

Two possible solutions are:


  • 将你的文件写入thename.xml.part,然后一旦完成/关闭重命名为thename.xml,这使得写入更接近原子 - 读者无法读取它直到它确实完成只要它只查找.xml文件。

  • 或者,使用数据库,原子性就是他们所做的。

这篇关于“文件过早结束” Java读取和写入XML数据文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆