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