http://blog.csdn.net/ablipan/article/details/8198692
使用SAXReader的read(File file)方法時,如果xml文件異常會導致文件被服務器占用不能移動文件,建議不使用read(File file)方法而使用read(FileInputStream fis)等流的方式讀取文件,異常時關閉流,這樣就不會造成流未關閉,文件被鎖的現象了。(在服務器中運行時會鎖住文件,main方法卻不會)。
1、以下方式xml文件異常時會導致文件被鎖
- Document document = null;
- File file = new File(xmlFilePath);
- SAXReader saxReader = new SAXReader();
- try
- {
- document = saxReader.read(file);
- } catch (DocumentException e)
- {
- logger.error("將文件[" + xmlFilePath + "]轉換成Document異常", e);
- }
2、以下方式xml文件異常時不會鎖文件(也可以使用其他的流來讀文件)
- Document document = null;
- FileInputStream fis = null;
- try
- {
- fis = new FileInputStream(xmlFilePath);
- SAXReader reader = new SAXReader();
- document = reader.read(fis);
- }
- catch (Exception e)
- {
- logger.error("將文件[" + xmlFilePath + "]轉換成Document異常", e);
- }
- finally
- {
- if(fis != null)
- {
- try
- {
- fis.close();
- } catch (IOException e)
- {
- logger.error("將文件[" + xmlFilePath + "]轉換成Document,輸入流關閉異常", e);
- }
- }
- }