public List<Element> readXml(String filePath){
List<Element> list = null;
FileInputStream fis = null;
FileChannel fc = null;
FileLock fl = null;
try{
// File file = new File(filePath);
fis = new FileInputStream(filePath);
fc = fis.getChannel();
fl = fc.tryLock(0,Long.MAX_VALUE,true);
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(fis);
Element rootElement = document.getRootElement();
list = rootElement.elements();
}catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(null != fl){
fl.release();
fl.close();
}
if(null != fc&&fc.isOpen()){
fc.close();
}
if(null != fis){
fis.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
return list;
}
saxReader用File方式读取XML后XML会一直被占用,后来改用FileInputStream,还是不行,最后就自己给文件加锁,读取完后,把锁释放,还是不行,文件一直被占用,求大神解决!javasaxReaderj2EEjavaWEBXML
List<Element> list = null;
FileInputStream fis = null;
FileChannel fc = null;
FileLock fl = null;
try{
// File file = new File(filePath);
fis = new FileInputStream(filePath);
fc = fis.getChannel();
fl = fc.tryLock(0,Long.MAX_VALUE,true);
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(fis);
Element rootElement = document.getRootElement();
list = rootElement.elements();
}catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(null != fl){
fl.release();
fl.close();
}
if(null != fc&&fc.isOpen()){
fc.close();
}
if(null != fis){
fis.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
return list;
}
saxReader用File方式读取XML后XML会一直被占用,后来改用FileInputStream,还是不行,最后就自己给文件加锁,读取完后,把锁释放,还是不行,文件一直被占用,求大神解决!javasaxReaderj2EEjavaWEBXML
解决方案 »
- hibernate id 自增长或者认为设置。
- webservice接口返回带"<>"字符的String如何不被转义
- jbpm报错,高手进来看看
- Apache/2.2.6 (Unix) PHP/5.2.3 mod_jk/1.2.25服务器出现错误,求大家帮忙看一下
- JMETER测试需要验证信息的webservice
- 如何实现下拉列表框中自己输入值?
- XML Schema中如何定义二进制字符串类型。
- hibernate插入数据出错 Table 'news.hibernate_unique_key' doesn't exist
- 为什么Java Web工程只能定制化?即只能是项目,不能是产品?
- weblogic里面删不掉部署的ejb怎么办?(在线等待)
- Hibernate3.3 Oracle10g HQL 查询使用 regexp_like 函数时不支持 在Dialect注册了之后也不行
- eclipse突然访问不了内网svn了,怎么回事?
* 读取XML
* @param filePath XML文件路径
* @return XML节点集
*/
@SuppressWarnings("unchecked")
public List<Element> readXml(String filePath){
List<Element> list = null;
FileInputStream fis = null;
try{
// File file = new File(filePath);
fis = new FileInputStream(filePath);
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(fis);
Element rootElement = document.getRootElement();
list = rootElement.elements();
}catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(null != fis){
fis.close();
System.gc();
Thread.sleep(5000);
}
}catch(Exception e){
e.printStackTrace();
}
}
return list;
}