您的位置:首页 > 其它

Dom4j SAXReader读取xml异常时占用文件,导致不能移动文件

2015-06-24 16:27 776 查看
原文地址: http://blog.csdn.net/ablipan/article/details/8198692
使用SAXReader的read(File file)方法时,如果xml文件异常会导致文件被服务器占用不能移动文件,建议不使用read(File file)方法而使用read(FileInputStream fis)等流的方式读取文件,异常时关闭流,这样就不会造成流未关闭,文件被锁的现象了。(在服务器中运行时会锁住文件,main方法却不会)。

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

[java] view
plaincopyprint?

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文件异常时不会锁文件(也可以使用其他的流来读文件)

[java] view
plaincopyprint?

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);

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: