如何确定文件是否正在被另一个进程使用(Java)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16010564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to determine if file is in use by another process (Java)
提问by Emanuele Mazzoni
I have tried many examples, but no one works. I try this but don't work.
我尝试了很多例子,但没有一个有效。我试试这个,但不起作用。
I tried also to use the tryLock(). It always returns false. why?
我也尝试使用tryLock(). 它总是返回false。为什么?
private boolean checkCompleteFile(File f)
{
RandomAccessFile file = null;
FileLock fileLock = null;
try
{
file = new RandomAccessFile(f, "rw");
FileChannel fileChannel = file.getChannel();
fileLock = fileChannel.lock();
if (fileLock != null)
{
fileLock.release();
file.close();
return false;
}
}
catch(Exception e)
{
return false;
}
return true;
}
回答by CloudyMarble
You catch an exception and return false, thats why you get false all the time, do something with the exception or do not catch it so you know if an exception was thrown, if you catch a general exception a false return value is not really meaningful.
你捕获一个异常并返回 false,这就是为什么你总是得到 false,对异常做一些事情或者不捕获它,这样你就知道是否抛出了异常,如果你捕获一个一般异常,一个 false 返回值并没有真正意义.
try {
lock = channel.tryLock();
// ...
} catch (OverlappingFileLockException e) {
// File is already locked in this thread or virtual machine
}
lock.release();
channel.close();
You cam just try to access the file and catch an exception if it fails:
您只需尝试访问该文件并在失败时捕获异常:
boolean isLocked=false;
RandomAccessFile fos=null;
try {
File file = new File(filename);
if(file.exists())
fos=new RandomAccessFile(file,"rw");
}catch (FileNotFoundException e) {
isLocked = true;
}catch (SecurityException e) {
isLocked = true;
}catch (Exception e) {
// handle exception
}finally {
try {
if(fos!=null) {
fos.close();
}
}catch(Exception e) {
//handle exception
}
}
Notice that the RandomAccessFileclass throws:
请注意,RandomAccessFile类抛出:
FileNotFoundException-
if the mode is "r" but the given string does not denote an existing regular file, or if the mode begins with "rw" but the given string does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file.
SecurityException-
if a security manager exists and its checkRead method denies read access to the file or the mode is "rw" and the security manager's checkWrite method denies write access to the file
FileNotFoundException-
如果模式为“r”但给定的字符串不表示现有的常规文件,或者模式以“rw”开头但给定的字符串不表示现有的可写常规文件并且该名称的新常规文件不能创建,或发生其他一些错误在打开或创建文件。
安全异常-
如果安全管理器存在且其 checkRead 方法拒绝对文件的读访问或模式为“rw”且安全管理器的 checkWrite 方法拒绝对文件的写访问
回答by StarCrafter
Try using this:
尝试使用这个:
try {
@SuppressWarnings("resource")
FileChannel channel = new RandomAccessFile(fileToRead, "rw").getChannel();
//This method blocks until it can retrieve the lock.
FileLock lock = channel.lock(); // Try acquiring the lock without blocking.
try {
lock = channel.tryLock();
} catch (OverlappingFileLockException e){
}
lock.release(); //close the file.
channel.close();
} catch (Exception e) {
}
回答by qrtt1
How about using the linux command ?
如何使用 linux 命令?
lsof -p
The command will show the file open status, you can parse it to check who use it.
该命令将显示文件打开状态,您可以解析它以查看谁使用它。

