且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查文件是否可以删除?

更新时间:2023-11-25 16:47:46

使用Write Lock打开文件。

Open the file with a Write Lock.

请参阅此处 http: //download.oracle.com/javase/6/docs/api/java/nio/channels/FileLock.html

FileChannel channel = new RandomAccessFile("C:\\foo", "rw").getChannel();

// Try acquiring the lock without blocking. This method returns
// null or throws an exception if the file is already locked.
FileLock lock = channel.tryLock();

// ...  

// release it
lock.release();