Results 1 to 3 of 3
  1. #1
    sumayaji is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default 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

  2. #2
    sumayaji is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default

    Please help me out in resolving this issue.

    -Suma

  3. #3
    JoseXXX is offline Member
    Join Date
    Jul 2011
    Posts
    1
    Rep Power
    0

    Default 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

Similar Threads

  1. lock desktop using java code
    By meghana in forum Forum Guides
    Replies: 3
    Last Post: 03-30-2010, 07:24 AM
  2. SWT program on Linux can find jar files.
    By glmarsh in forum SWT / JFace
    Replies: 1
    Last Post: 07-20-2009, 08:02 PM
  3. unable to import jar files (crystal report 11)
    By kishore101 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-25-2009, 08:51 AM
  4. loading flash files in linux
    By rajeshang in forum AWT / Swing
    Replies: 0
    Last Post: 05-31-2008, 02:59 PM
  5. Files not completely deleted on Linux
    By amamare in forum Advanced Java
    Replies: 0
    Last Post: 12-19-2007, 03:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •