检查文件是否已经打开 [英] Check if file is already open

查看:170
本文介绍了检查文件是否已经打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要写一个自定义的批处理文件重命名。我已经完成了大部分工作,除非我无法弄清楚如何检查文件是否已经打开。我只是使用 java.io.File 包,并有一个 canWrite()方法,似乎测试该文件是否被另一个程序使用。任何想法,我怎么能做到这一点?

解决方案

使用Apache Commons IO库...

  boolean isFileUnlocked = false; 
尝试{
org.apache.commons.io.FileUtils.touch(yourFile);
isFileUnlocked = true;
} catch(IOException e){
isFileUnlocked = false;


if(isFileUnlocked){
//做一些你需要做的事情,那就是不锁定的文件。
} else {
//做一些你需要处理的文件

$ / code>


I need to write a custom batch File renamer. I've got the bulk of it done except I can't figure out how to check if a file is already open. I'm just using the java.io.File package and there is a canWrite() method but that doesn't seem to test if the file is in use by another program. Any ideas on how I can make this work?

解决方案

Using the Apache Commons IO library...

boolean isFileUnlocked = false;
try {
    org.apache.commons.io.FileUtils.touch(yourFile);
    isFileUnlocked = true;
} catch (IOException e) {
    isFileUnlocked = false;
}

if(isFileUnlocked){
    // Do stuff you need to do with a file that is NOT locked.
} else {
    // Do stuff you need to do with a file that IS locked
}

这篇关于检查文件是否已经打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆