2012-05-28 45 views
4

如何知道文件的状态,即如果应用程序被阻塞并且阻塞。在Java中,如果有这样的API或者您可以使用Java IO。java中文件的状态

谢谢。

回答

4

如果你问有关文件锁,从Example Depot下面的例子演示了如何使用它:

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

确定,但我怎么能知道哪个应用程序阻止它?捕捉异常,我可以告诉? – josemm1790

+0

对不起,API没有说明什么:( –

+0

好的谢谢,我会尝试。 – josemm1790