Rising Sun

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  148 随笔 :: 0 文章 :: 22 评论 :: 0 Trackbacks
http://blog.csdn.net/ablipan/article/details/8198692

使用SAXReader的read(File file)方法时,如果xml文件异常会导致文件被服务器占用不能移动文件,建议不使用read(File file)方法而使用read(FileInputStream fis)等流的方式读取文件,异常时关闭流,这样就不会造成流未关闭,文件被锁的现象了。(在服务器中运行时会锁住文件,main方法却不会)。


1、以下方式xml文件异常时会导致文件被锁

  1.                    Document document = null;  
  2. File file = new File(xmlFilePath);  
  3. SAXReader saxReader = new SAXReader();  
  4. try  
  5. {  
  6.     document = saxReader.read(file);  
  7. } catch (DocumentException e)  
  8. {  
  9.     logger.error("将文件[" + xmlFilePath + "]转换成Document异常", e);  
  10. }  


2、以下方式xml文件异常时不会锁文件(也可以使用其他的流来读文件)

  1.                 Document document = null;  
  2. FileInputStream fis = null;  
  3. try  
  4. {  
  5.     fis = new FileInputStream(xmlFilePath);  
  6.     SAXReader reader = new SAXReader();  
  7.     document = reader.read(fis);  
  8. }   
  9. catch (Exception e)  
  10. {  
  11.     logger.error("将文件[" + xmlFilePath + "]转换成Document异常", e);  
  12. }   
  13. finally  
  14. {  
  15.     if(fis != null)  
  16.     {  
  17.         try  
  18.         {  
  19.             fis.close();  
  20.         } catch (IOException e)  
  21.         {  
  22.             logger.error("将文件[" + xmlFilePath + "]转换成Document,输入流关闭异常", e);  
  23.         }  
  24.     }  
  25. }  
posted on 2014-09-02 14:00 brock 阅读(459) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: