各位,我想问一下用java如何才能锁定一个文件夹,好像用FileChannel,FileLock可以锁定一个文件,但如何才能锁定一个文件夹呢!!!!!

解决方案 »

  1.   

    应该是支持的吧,我在网上能找到VB锁定文件夹的例子,不过java的貌似找不到!!
      

  2.   

     Java进程内可以进行锁定,但是外部程序一样可以访问
      

  3.   


        try {
            // Get a file channel for the file
            File file = new File("filename");
            FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
        
            // Use the file channel to create a lock on the file.
            // This method blocks until it can retrieve the lock.
            FileLock lock = channel.lock();
        
            // Try acquiring the lock without blocking. This method returns
            // null or throws an exception if the file is already locked.
            try {
                lock = channel.tryLock();
            } catch (OverlappingFileLockException e) {
                // File is already locked in this thread or virtual machine
            }
        
            // Release the lock
            lock.release();
        
            // Close the file
            channel.close();
        } catch (Exception e) {
        }