Java-Desktop.getDesktop().open()-等待文件打开? [英] Java - Desktop.getDesktop().open() - Wait until file is open?
本文介绍了Java-Desktop.getDesktop().open()-等待文件打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种解决方案,以等待文件打开.我的应用程序打开pdf文件并显示用户输入对话框,但该对话框与pdf文件重叠.pdf文件完全打开时,是否可以添加侦听器或其他方法来显示我的对话框?
I'm searching for a solution to wait until a file is opened. My application opens pdf files and displays user-input dialogs but the dialog is overlapped by the pdf file. Is there a way to add a listener or something to show my dialog when the pdf file is fully open?
我可以使用延迟或暂停,但这并不是我想要的.
I could use a delay or pause but that's not exactly what I want.
我正在使用
Desktop.getDesktop().open(new File("my.pdf"));
推荐答案
要打开我刚刚创建的文件,请使用以下方法:
To open a file I just created, I use this method:
// Returns true if the file is unlocked or if it becomes unlocked in the next x seconds
public static boolean isFileUnlockedWithTimeout(File file, int timeoutInSeconds) {
if (!file.exists())
// The file does not exist. So it's not locked. (Another approach could be to throw an exception...)
return true;
long endTime = System.currentTimeMillis() + 1000 * timeoutInSeconds;
while (true) {
if (file.renameTo(file)) {
// The file is not locked
return true;
}
if (System.currentTimeMillis() > endTime) {
// After the timeout
return false;
}
// Wait 1/4 sec.
try { TimeUnit.MILLISECONDS.sleep(250); } catch (InterruptedException e) {}
}
}
适用于第一个问题:
File myFile = new File("my.pdf");
if (isFileUnlockedWithTimeout(myfile, 5)) {
// File is not locked, we can open it
Desktop.getDesktop().open(myFile);
} else {
System.out.println("File is locked. Cannot open.");
}
这篇关于Java-Desktop.getDesktop().open()-等待文件打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文