0

In my project, i need to imeplement file locking and the problem is that we need to use java 6 only so what are the ways through which i can implement file locking for both platform(windows/linux)

user207421
  • 305,947
  • 44
  • 307
  • 483
mayank agrawal
  • 628
  • 5
  • 20

1 Answers1

0

You can use nio API for that. Find the below code snippet. It will help you to solve your problem

FileInputStream in = new FileInputStream(file);
try {
    java.nio.channels.FileLock lck = in.getChannel().lock();
    try {
        Reader reader = new InputStreamReader(in, charset);
        ...
    } finally {
        lck.release();
    }
} finally {
    in.close();
}

For more details you can refer java docs for FileLock https://docs.oracle.com/javase/8/docs/api/java/nio/channels/FileLock.html#pdep

Ankush
  • 78
  • 10