java - Prevent file channel from closing after reading xml file -


For more detailed information about the motivation behind this goal (and my efforts to solve it) see mine Decided to ask it as a new question as I thought it was sufficiently developed for the ability to do this. In essence, I intend to use JDOM in conjunction with NIO:

  1. Get a special file lock on an XML file.
  2. File the document object.
  3. Make unchecked changes (still active with lock!).
  4. Write changes back into the XML file.
  5. Release the file lock.

The problem I'm having is that the underlying code closes the channel to read the XML file in the document object (and therefore releases the lock), as seen below Gone:

  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 document filterfinder dbftff; Private Document Builder Debuilder; Private document doctor; Public Test 4 (Try (Last file channel channel = new RandomAccessFile (new file (path), "RW"). Mixing ()) {dbFactory = DocumentBuilderFactory.newInstance (); DBuilder = dbFactory.newDocumentBuilder (); Println Channel.isOpen ()); doc = dbuilder.parse (channel.NewNotStream (channel)); println (channel.isOpen ()); channel.close ();} hold (IOException | ParserConfigurationException | SAXException e) {e.printStackTrace ();}} Public static zero main (string [] args) {new test 4 ();}}  

output:

  true false < / Code> 

viewed through the documentation and the Java book Trawled in rhythm, I actually also find out where the channel is closed, how to stop it from closing alone, no sign will be great! Thanks.

There is a clear way to do this: Override a FilterInputStream and off to do anything else:

>
  public trial ({{channel = new RandomAccessFile (new file (path), "RW"). Getchannel (); DbFactory = DocumentBuilderFactory.newInstance (); DBuilder = dbFactory.newDocumentBuilder (); Println (channel.isOpen ()); NonClosingInputStream ncis = New Non-Closing InStream (Channel.NewNotStream (Channel)); Doctor = DeBuilder Parse (NCIS); Println (channel.isOpen ()); // closes here ncis.reallyClose (); Channel.close (); // redundant} Cache (IOException | ParserConfigurationException | SAXException e) {e.printStackTrace (); }} NonClosing InputStream FilterInputStream {Public NonClosingInputStream Extensions (This InputStream) {Super (This); } @ Override Public Wide Close () IOException throws {// nothing} Public null Actually close () IOException throws {// really close In.close (); }}  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -