java中文件的状态

java中文件的状态,java,io,Java,Io,如何知道文件的状态,即应用程序是否被阻止以及它是否被阻止。在java中,如果有用于此的API,或者您可以使用java IO 谢谢。如果您询问有关文件锁的问题,下面的示例将演示如何使用它: try { // Get a file channel for the file File file = new File("filename"); FileChannel channel = new RandomAccessFile(file, "rw").getChannel();

如何知道文件的状态,即应用程序是否被阻止以及它是否被阻止。在java中,如果有用于此的API,或者您可以使用java IO


谢谢。

如果您询问有关文件锁的问题,下面的示例将演示如何使用它:

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) {
}

好的,但是我怎么知道哪个应用程序阻止了它呢?捕捉到我能告诉你的异常?抱歉,API没有声明任何关于这一点的内容:(应用程序可能不知道,但我阻止它解决了主要问题。非常感谢。