在java中流关闭异常 [英] Stream closed exception in java
问题描述
请看看我做了什么
private InputSource getContent(String fName) throws SAXException, IOException, ServiceException {
// Some code here
if(currentNodeRef != null)
{
ContentReader reader = contentService.getReader(currentNodeRef,ContentModel.PROP_CONTENT);
InputStream inputStream = null;
try
{
inputStream = new BufferedInputStream(reader.getContentInputStream(),16384);
return new InputSource(inputStream);
}
finally
{
if(inputStream!=null)
try
{
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return new InputSource();
}
在我的parseDocument方法中,我调用了上述方法。
In my parseDocument method I called the above method.
parseDocRoot(getContent(fName),path);
在parseDocRoot中
In parseDocRoot
public void parseDocRoot (InputSource ins, String path) throws SAXException, IOException,
ParserConfigurationException, ServiceException
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId)
throws SAXException, IOException {
return new InputSource(new ByteArrayInputStream(new byte[0]));
}
});
Document doc = builder.parse(ins);
NodeList list = doc.getChildNodes();
parseDocument(list,path);
}
我收到错误说流不关闭,同时调试上面的代码我发现错误就行了
I got a error Saying that Stream Not closed and while debugging the above code I found that the error is on the line
Document doc = builder.parse(ins);
请帮我找到解决方案。
推荐答案
我认为错误是流 已关闭。原因是你有一个finally块:
I believe the error is that the stream is closed. The reason is that you have a finally block:
try
{
inputStream = new BufferedInputStream(reader.getContentInputStream(),16384);
return new InputSource(inputStream);
}
finally
{
if(inputStream!=null)
try
{
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
你关闭流在finally块中。此代码在从方法返回之前立即执行。重要的是,在方法返回并由 parse
方法处理之前,InputStream不会消耗。
You close the stream in the finally block. This code gets executed immediately before returning from the method. Importantly, the InputStream isn't consumed until after the method returns and is processed by the parse
method.
您应该将finally块放在整个代码块周围 - 在实际完成时关闭流。最简单的方法是内联 getContent
方法,并在调用 parse
后放入finally块。在你这样做之后,你或许可以找到一种方法来封装那个逻辑,但它会有点棘手,因为你必须保持一个InputStream句柄,直到你完成解析,这样你就可以关闭它。
You should instead put the finally block around the entire block of code -- closing the stream when you are actually done. The easiest way to do that would be to inline your getContent
method and put the finally block after calling parse
. After you do that, you might be able to figure out a way to encapsulate that logic, but it'll be kind of tricky since you definitely need to keep a handle the the InputStream until you're done parsing so that you can close it.
另一个更简单的选择是使 getContent
返回文档
而不是简单地在该方法中移动 parseDocRoot(getContent(fName),path);
。
Another far simpler option would be to make getContent
return a Document
instead by simply moving parseDocRoot(getContent(fName),path);
inside that method.
这篇关于在java中流关闭异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!