刚才说错了,不好意思。CrazyGou(阿狗)(青春拉扯着我们,轰轰烈烈地向前跑。) 方法中:File file = new File(path); RandomAccessFile raf = new RandomAccessFile(file, "rw"); FileChannel fc = raf.getChannel(); //FileLock lock = fc.tryLock(); FileLock lock = fc.lock(); ... lock.release(); raf.close();这个方法是可以的。下面是java doc里面的原话 This file-locking API is intended to map directly to the native locking facility of the underlying operating system. Thus the locks held on a file should be visible to all programs that have access to the file, regardless of the language in which those programs are written. 翻译: 此文件锁定 API 直接映射到基础操作系统的本机锁定设施。因此,无论程序是用何种语言编写的,某个文件上所保持的锁定对于所有访问该文件的程序来说都应该是可见的。 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~非常感谢!!!! 不过比较郁闷的是 从以下版本开始: 1.4 由于某些原因只有用1.3的JDK请问还有没有其他的方法?
File file = new File(path);
RandomAccessFile raf = new RandomAccessFile(file, "rws") //以独占方式打开文件
...
raf.close();
或
File file = new File(path);
RandomAccessFile raf = new RandomAccessFile(file, "rw");
FileChannel fc = raf.getChannel();
//FileLock lock = fc.tryLock();
FileLock lock = fc.lock();
...
lock.release();
raf.close();
但对不同程序,我试了一下似乎 不起作用
还是谢谢各位。gongyali2005(JAVA 民工) 能否给个具体的例子?
RandomAccessFile raf = new RandomAccessFile(file, "rw");
FileChannel fc = raf.getChannel();
//FileLock lock = fc.tryLock();
FileLock lock = fc.lock();
...
lock.release();
raf.close();这个方法是可以的。下面是java doc里面的原话 This file-locking API is intended to map directly to the native locking facility of the underlying operating system. Thus the locks held on a file should be visible to all programs that have access to the file, regardless of the language in which those programs are written.
翻译:
此文件锁定 API 直接映射到基础操作系统的本机锁定设施。因此,无论程序是用何种语言编写的,某个文件上所保持的锁定对于所有访问该文件的程序来说都应该是可见的。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~非常感谢!!!!
不过比较郁闷的是 从以下版本开始: 1.4
由于某些原因只有用1.3的JDK请问还有没有其他的方法?