一种干净的方法是创建一个
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(); }}


