Unable to lock files on linux using java.nio.channels.FileLock
Hi,
I'm new to java.nio . I wanted to explore on FileLocking that is provided by java.nio.Channels.FileLock. I tried this out on Windows and it worked well. But I found that FileLock was not happening when it came to linux(used same program that I had used on Windows). Is there any platform dependency? Or any configurations that is missing out in linux?
Please help me out in getting this Filelock on Linux.
Writer code .....................
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
public class Writer {
public static void main(String args[]){
try {
File f = new File("D:\\Test\\a.txt");
FileChannel fc = new RandomAccessFile(f, "rw").getChannel();
FileLock fl = fc.lock();
System.out.println("Locked by writer");
long startTime = System.currentTimeMillis();
int i=0;
try {
while(System.currentTimeMillis() - startTime < 20000 ){
ByteBuffer bf = ByteBuffer.allocate(1024);
String s = "testing"+(i++) + "\n";
for (int j=0;j<s.toCharArray().length ;j++) {
bf.put((byte)s.toCharArray()[j]);
}
bf.flip();
fc.write(bf);
fc.force(false);
}
} catch (OverlappingFileLockException e) {
System.out.println("File is already locked in this thread or virtual machine");
}
// Release the lock
fl.release();
// Close the file
fc.close();
} catch (IOException e) {
System.out.println("Exception:"+e.getMessage());
}
}
}
Reader Code ............................
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
public class Reader {
public static void main(String args[]){
File f = null;
FileInputStream fi = null;
FileChannel fc = null;
try {
f = new File("D:\\Test\\a.txt");
fi = new FileInputStream(f);
fc = fi.getChannel();
}catch(FileNotFoundException e){
System.out.println(e.getMessage());
}
try{
ByteBuffer bf = ByteBuffer.allocate((int)fc.size());
byte[] b = new byte[(int)fc.size()];
fc.read(bf);
bf.flip();
bf.get(b);
System.out.println(new String(b));
fc.close();
}catch(IOException e){
System.out.println("setting to false");
System.out.println(e.getMessage());
}catch(OverlappingFileLockException eo){
System.out.println("setting to false");
System.out.println(eo.getMessage());
}
}
}
Writer was executed first and then the Reader. On Windows when Reader started, I got....
"The process cannot access the file because another process has locked a portion of the file"
-Suma
Were you able to solve this issue ?
Hi,
Were you able to solve this issue ? I am having the same problem with java version "1.6.0_18" and Ubuntu 10.10 2.6.35-22-generic.
Best Regards,
Jose