你想要一个FileLock:
FileChannel channel = new RandomAccessFile("C:\foo", "rw").getChannel();// Try acquiring the lock without blocking. This method returns// null or throws an exception if the file is already locked.FileLock lock = channel.tryLock();// ...// release itlock.release();为了简单起见,我省略了一个try / finally块,但您不应该在生产代码中使用



