简体   繁体   English

如何在Java中实现文件夹锁定

[英]how to implement the folder lock in java

i want lock the particular folder, and i have code but the "java.io.FileNotFoundException: (Access is denied)" error is found 我想锁定特定的文件夹,并且我有代码,但是发现“ java.io.FileNotFoundException :(访问被拒绝)”错误

public class Folder_Lock {

    public static void main(String[] args) {

    FileLock lock = null;
    FileChannel channel = null;
        try {
            // Get a file channel for the file

            File file = new File("C:\\Users\\kaizen\\Desktop\\mani1");

            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.
            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


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (lock!=null) try { lock.release(); } catch (IOException e) { }
            // Close the file
            if (channel!=null) try { channel.close(); } catch (IOException e) { }
        }

    }
}

can any one solve the issue 谁能解决这个问题

You need to add an exception Handler to handle the exception. 您需要添加一个异常处理程序来处理该异常。 In

File file = new File("C:\\Users\\kaizen\\Desktop\\mani1.addExtension");

This will resolve your issue. 这样可以解决您的问题。

尝试通过admin或First Run IDE以admin身份运行您的文件夹,然后运行文件,因为需要ur in C:/系统访问权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM