我正在尝试检查某个java.io.File是否由外部程序打开。在Windows上我使用这个简单的技巧:
try {
FileOutputStream fos = new FileOutputStream(file);
// -> file was closed
} catch(IOException e) {
// -> file still open
}
我知道基于unix的系统允许在多个进程中打开文件...是否有类似的技巧可以为基于unix的系统实现相同的结果?
任何帮助/黑客高度赞赏: - )
答案 0 :(得分:9)
以下是基于unix的系统如何使用 lsof 的示例:
public static boolean isFileClosed(File file) {
try {
Process plsof = new ProcessBuilder(new String[]{"lsof", "|", "grep", file.getAbsolutePath()}).start();
BufferedReader reader = new BufferedReader(new InputStreamReader(plsof.getInputStream()));
String line;
while((line=reader.readLine())!=null) {
if(line.contains(file.getAbsolutePath())) {
reader.close();
plsof.destroy();
return false;
}
}
} catch(Exception ex) {
// TODO: handle exception ...
}
reader.close();
plsof.destroy();
return true;
}
希望这有帮助。
答案 1 :(得分:3)
您可以从Java程序运行lsof
Unix实用程序,该实用程序可以告诉您哪个进程正在使用文件,然后分析其输出。要从Java代码运行程序,请使用例如Runtime
,Process
,ProcessBuilder
类。注意:在这种情况下,您的Java程序将无法移植,这与可移植性概念相矛盾,因此请三思而后行是否真的需要:)
答案 2 :(得分:3)
这个也适用于Windows系统。但是注意,对Linux不起作用!
private boolean isFileClosed(File file) {
boolean closed;
Channel channel = null;
try {
channel = new RandomAccessFile(file, "rw").getChannel();
closed = true;
} catch(Exception ex) {
closed = false;
} finally {
if(channel!=null) {
try {
channel.close();
} catch (IOException ex) {
// exception handling
}
}
}
return closed;
}
答案 3 :(得分:1)
您可以通过@ZZ Coder
尝试使用此信号量类型代码进行文件锁定File file = new File(fileName);
FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
FileLock lock = channel.lock();
try {
lock = channel.tryLock();
// Ok. You get the lock
} catch (OverlappingFileLockException e) {
// File is open by someone else
} finally {
lock.release();
}
答案 4 :(得分:1)
感谢您的原始建议。我有一个小升级对这个方法有点重要:
FileOutputStream fos = null;
try {
// Make sure that the output stream is in Append mode. Otherwise you will
// truncate your file, which probably isn't what you want to do :-)
fos = new FileOutputStream(file, true);
// -> file was closed
} catch(IOException e) {
// -> file still open
} finally {
if(fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
干杯, Gumbatron