I want to write an XML data inside my locked file I have a logic like this, but my file data isn't replaced by this text data and i got exception like
this:Exception in thread "main" java.nio.channels.ClosedChannelException at sun.nio.ch.FileChannelImpl.ensureOpen(FileChannelImpl.java:110) at sun.nio.ch.FileChannelImpl.write(FileChannelImpl.java:199) at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:218) at sun.nio.cs.StreamEncoder.implClose(StreamEncoder.java:316) at sun.nio.cs.StreamEncoder.close(StreamEncoder.java:149) at java.io.BufferedWriter.close(BufferedWriter.java:266) at java_io_Closeable$close.call(Unknown Source) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117) at locckGroovy.main(loccky.groovy:90)
. What should I change to make this?
RandomAccessFile ini = new RandomAccessFile(file, "rwd");
FileLock lock = ini.getChannel().tryLock();
try{
w=new BufferedWriter(Channels.newWriter(ini.getChannel(),"UTF-8"));
w.write(text);
}finally{
ini.close();
}