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 閱讀(493) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 铁岭市| 高州市| 华安县| 元江| 榆中县| 龙口市| 和政县| 突泉县| 固始县| 正阳县| 谢通门县| 喀喇沁旗| 七台河市| 遵化市| 车险| 隆林| 故城县| 页游| 陆河县| 来宾市| 仪征市| 关岭| 祁东县| 五河县| 乐东| 武胜县| 平江县| 叶城县| 伊川县| 佛学| 寿光市| 望谟县| 彰化县| 鸡东县| 芦山县| 昌宁县| 靖边县| 府谷县| 屏东县| 丰原市| 腾冲县|