SAXBuilder不释放文件句柄的问题

参考:

http://bbs.csdn.net/topics/370099985

http://osdir.com/ml/java.jdom.general/2004-11/msg00031.html


问题描述:

有人报告 SAXBuilder 存在bug:打开文件后并不释放文件资源,结果多次打开同一个文件后,会导致”too many open files“错误。


解决:

将下面的做法

SAXBuilder builder = new SAXBuilder();
File xmlFile = new File(path);
Document document = (Document) builder.build(xmlFile);

改成这样

InputStream xmlFile = null;
try {
	SAXBuilder builder = new SAXBuilder();
	xmlFile = new FileInputStream(your_file_path);
	Document document = (Document) builder.build(xmlFile);
} catch (...) {
	...
} finally {
	try {
		xmlFile.close();
	} catch (IOException e) {
		...
	}
}
即,在finally 代码块中,强制释放文件资源。


你可能感兴趣的:(SAXBuilder不释放文件句柄的问题)