4

我想锁定特定文件夹,并且我有代码,但发现“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) { }
        }

    }
}

任何人都可以解决这个问题

4

2 回答 2

2

你需要添加一个异常Handler来处理异常。在

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

这将解决您的问题。

于 2015-01-13T07:16:16.567 回答
0

尝试通过管理员或首次以管理员身份运行 IDE 运行您的文件夹,然后运行该文件,因为需要 C:/ 系统访问权限

于 2013-11-18T10:06:07.723 回答