For more information on the motivation for this goal (and my attempts to solve it), review the previous question. I decided to ask this as a new question in its entirety, as I thought it had developed enough to deserve it. As a summary, I intend to use JDOM in combination with NIO to:
- Get an exclusive file lock in an XML file.
- Read the file in
Document. - Arbitrary changes (with the lock still active!).
- Write the changes to the xml file.
- Release the file lock.
However, the problem I get is that the inline code to read the XML file into the document object closes the pipe (and therefore releases the lock), as shown below:
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();
}
}
Conclusion:
true
false
Java-, , , , . ! .