我需要帮助的只是测试文件是否已打开。
这是我所拥有的:
public static void main(String[] args) {
//Prompt user to input file name
SimpleIO.prompt("Enter File name: ");
String fileName = SimpleIO.readLine();
//Create file object
File file = new File (fileName);
//Check to see if file is opened
if (!file.exists()){
System.out.println("The file you entered either do not exist or the name is spelled wrong.\nProgram is now being terminated.\nGoodbye!");}
}
Run Code Online (Sandbox Code Playgroud)
如果这
//Create file object
File file = new File (fileName);
Run Code Online (Sandbox Code Playgroud)
不产生异常,则文件被正确访问。但是,如果您需要检查文件是否正在写入或是否已以其他方式被访问,则需要检查它是否已锁定。
File file = new File(fileName);
FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
FileLock lock = channel.lock();
try {
lock = channel.tryLock();
System.out.print("file is not locked");
} catch (OverlappingFileLockException e) {
System.out.print("file is locked");
} finally {
lock.release();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10764 次 |
| 最近记录: |