简体   繁体   English

XMLStreamReader没有关闭打开的xml文件

[英]XMLStreamReader not closing opened xml file

To use XMLStreamReader I am initializing it like - 要使用XMLStreamReader我正在初始化它像 -

XMLInputFactory f = XMLInputFactory.newInstance();
XMLStreamReader reader = f.createXMLStreamReader(new FileReader(
        "somefile.xml"));

Iterating over it like - 迭代它像 -

if (reader.hasNext()) {
    reader.next();
    // do something with xml data
}

Finally closing it like - 最后关闭它像 -

reader.close();

This looks to be a normal flow but I am seeing some strange behavior. 这看起来是正常的流程,但我看到一些奇怪的行为。 Even after closing reader, OS doesn't allow me delete/move the xml file unless I exit from java program. 即使在关闭阅读器之后,OS也不允许我删除/移动xml文件,除非我退出java程序。 When run on Win2k8-server, I get error message saying java.exe is using this xml file. 在Win2k8服务器上运行时,我收到错误消息,说java.exe正在使用此xml文件。

So I have couple of questions - 所以我有几个问题 -

  1. Do I need to explicitly close each FileReader's close? 我是否需要明确关闭每个FileReader的关闭?
  2. How can I find out which java code path is keeping this file handle open. 如何找出保持此文件句柄处于打开状态的java代码路径。

Looking @ the documentation of XMLStreamReader's close(), I get following - "Frees any resources associated with this Reader. This method does not close the underlying input source." 看看XMLStreamReader的close()文档,我得到以下内容 - “释放与此Reader相关的任何资源。此方法不会关闭底层输入源。”

What is the meaning of "underlying input source"? “底层输入源”是什么意思? Why is that not closed by reader's close()? 为什么不被读者关闭()?

The underlying input source mentioned in the doc is exactly what you should close. 文档中提到的基础输入源正是您应该关闭的内容。 Put the FileReader into a local variable to be able to close it: 将FileReader放入局部变量以便能够关闭它:

XMLInputFactory f = XMLInputFactory.newInstance();
FileReader fr = new FileReader("somefile.xml");
XMLStreamReader reader = f.createXMLStreamReader(fr);

// process xml

reader.close();
fr.close();

//suggest using apache commons IOUtils.closeQuietly(fr); this way you
// don't have to deal with exceptions if you don't want

What is the meaning of "underlying input source"? “底层输入源”是什么意思? Why is that not closed by reader's close()? 为什么不被读者关闭()?

You created XMLReader by XMLInputFactory.createXMLStreamReader with argument InputStream or so. 您通过XMLInputFactory.createXMLStreamReader使用参数InputStream创建了XMLReader。 This is an "underlying input source". 这是一个“潜在的输入源”。 :) Because it opened outside of XMLReader, thus XMLReader will not close it. :)因为它在XMLReader之外打开,因此XMLReader不会关闭它。 But, if you need, can use this wrapper class for XMLReader. 但是,如果需要,可以将此包装类用于XMLReader。 Just works. 只是工作。

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.util.StreamReaderDelegate;

public class XMLFileReader extends StreamReaderDelegate implements AutoCloseable {
    private final static XMLInputFactory factory = XMLInputFactory.newFactory();
    private final Path file;
    private final InputStream stream;
    private final XMLStreamReader reader;

    public XMLFileReader(Path file) throws IOException, XMLStreamException {
        super();
        this.file = file;
        stream = Files.newInputStream(this.file);
        reader = factory.createXMLStreamReader(stream);
        setParent(reader);
    }

    @Override
    public void close() throws XMLStreamException {
        try {
            super.close();
            stream.close();
        } catch (IOException e) {
            throw new XMLStreamException(file+" : "+e.getMessage(),e);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM