gpt4 book ai didi

java - 为什么 StAX 类没有在 Java 7 中针对 ARM 进行改造

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:47:03 24 4
gpt4 key购买 nike

我希望在 Java 7 中发现 XMLStreamReaderAutoCloseable。然而,事实并非如此。是否有技术原因导致 StAX 读写器接口(interface)没有(或不应该) retrofit 以实现 AutoCloseable ?他们已经有了close方法,其意图与AutoCloseable的close方法没有区别。

最佳答案

如果你仔细观察 close() method of AutoCloseable :

Closes this resource, relinquishing any underlying resources. This method is invoked automatically on objects managed by the try-with-resources statement.

甚至Closeable close() method :

Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.

close() method of XMLStreamReader说:

Frees any resources associated with this Reader. This method does not close the underlying input source.

实际上,输入源是由 Reader 管理的它实现了 Closeable 接口(interface)。因此,读者可以在 try-with-ressource 中接近。

例如:

    XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader reader = null;
try (FileReader fr = new FileReader("file.xml")) { //Will close the FileReader
reader = factory.createXMLStreamReader(fr);
reader.close();
}
catch (XMLStreamException ex) {
if(reader!=null)try {
reader.close();
} catch (XMLStreamException ex1) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex1);
}
}

关于java - 为什么 StAX 类没有在 Java 7 中针对 ARM 进行改造,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10537258/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com