有关此目标背后的动机(以及我解决此问题的努力)的详细信息,请查看我的previous question。我决定将这个问题完全视为一个新问题,因为我认为它已经发展到足够值得这样做。总而言之,我打算将JDOM与NIO结合使用,以便:
Document
对象。然而,我得到的问题是,将xml文件读入文档对象的内置代码会关闭通道(因此会释放锁定),如下所示:
import java.io.*;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import javax.xml.parsers.*;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class Test4{
String path = "Test 2.xml";
private DocumentBuilderFactory dbFactory;
private DocumentBuilder dBuilder;
private Document doc;
public Test4(){
try (final FileChannel channel = new RandomAccessFile(new File(path), "rw").getChannel()) {
dbFactory = DocumentBuilderFactory.newInstance();
dBuilder = dbFactory.newDocumentBuilder();
System.out.println(channel.isOpen());
doc = dBuilder.parse(Channels.newInputStream(channel));
System.out.println(channel.isOpen());
channel.close();
} catch (IOException | ParserConfigurationException | SAXException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
new Test4();
}
}
输出:
true
false
查看了文档并浏览了内置的Java库,我真的很难找到关闭通道的位置,更不用说如何阻止它关闭了。任何指针都会很棒!感谢。
答案 0 :(得分:4)
执行此操作的一种简洁方法是创建FilterInputStream
并覆盖close
无所作为:
public Test() {
try {
channel = new RandomAccessFile(new File(path), "rw").getChannel();
dbFactory = DocumentBuilderFactory.newInstance();
dBuilder = dbFactory.newDocumentBuilder();
System.out.println(channel.isOpen());
NonClosingInputStream ncis = new NonClosingInputStream(Channels.newInputStream(channel));
doc = dBuilder.parse(ncis);
System.out.println(channel.isOpen());
// Closes here.
ncis.reallyClose();
channel.close(); //Redundant
} catch (IOException | ParserConfigurationException | SAXException e) {
e.printStackTrace();
}
}
class NonClosingInputStream extends FilterInputStream {
public NonClosingInputStream(InputStream it) {
super(it);
}
@Override
public void close() throws IOException {
// Do nothing.
}
public void reallyClose() throws IOException {
// Actually close.
in.close();
}
}
答案 1 :(得分:-1)
您是否尝试过try-with-ressource声明?即使这不是这个功能的主要目的,也许这就是你正在寻找的东西(以自动关闭资源(你的频道)的形式,但只有当你离开相应的试块时)
try (final Channel channel = new RandomAccessFile(new File(path), "rw").getChannel()) {
// your stuff here
} catch (IOException ex) {
ex.printStackTrace();
}