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)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 乌拉特前旗| 武穴市| 土默特左旗| 伊宁市| 中方县| 咸丰县| 宝应县| 桑植县| 镇宁| 新田县| 贺州市| 阳城县| 辽阳市| 华安县| 如皋市| 东辽县| 揭东县| 孝感市| 开封县| 韶山市| 广宁县| 泰兴市| 喀喇沁旗| 阿勒泰市| 桐柏县| 湖州市| 仁寿县| 松原市| 隆昌县| 安达市| 镇远县| 本溪市| 南昌县| 岳普湖县| 南漳县| 镇巴县| 安塞县| 维西| 准格尔旗| 阳泉市| 克拉玛依市|