Java-Desktop.getDesktop().open()-是否等待文件打开?
我正在搜索一个解决方案,以等待文件打开。我的应用程序打开pdf文件并显示用户输入对话框,但该对话框与pdf文件重叠。当pdf文件完全打开时,有没有办法添加一个侦听器或其他东西来显示我的对话框Java-Desktop.getDesktop().open()-是否等待文件打开?,java,file,desktop,Java,File,Desktop,我正在搜索一个解决方案,以等待文件打开。我的应用程序打开pdf文件并显示用户输入对话框,但该对话框与pdf文件重叠。当pdf文件完全打开时,有没有办法添加一个侦听器或其他东西来显示我的对话框 我可以延迟或暂停,但这不是我想要的 我正在使用 Desktop.getDesktop().open(new File("my.pdf")); 如果您知道桌面打开PDF文件所需的时间,则可以使用 import java.util.Timer; ... Timer timer = new Timer(); .
我可以延迟或暂停,但这不是我想要的 我正在使用
Desktop.getDesktop().open(new File("my.pdf"));
如果您知道桌面打开PDF文件所需的时间,则可以使用
import java.util.Timer;
...
Timer timer = new Timer();
...
Desktop.getDesktop().open(new File("my.pdf"));
int openTime = 10*1000; //Let's say it would take 10s to be opened
timer.schedule(new TimerTask() {
@Override
public void run() {
//Code to show the dialog you need
}
}, openTime);
如果您知道桌面打开PDF文件所需的时间,则可以使用
import java.util.Timer;
...
Timer timer = new Timer();
...
Desktop.getDesktop().open(new File("my.pdf"));
int openTime = 10*1000; //Let's say it would take 10s to be opened
timer.schedule(new TimerTask() {
@Override
public void run() {
//Code to show the dialog you need
}
}, openTime);
要打开我刚刚创建的文件,我使用以下方法:
// 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.");
}
要打开我刚刚创建的文件,我使用以下方法:
// 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.");
}
“我可以使用延迟或暂停,但这不完全是我想要的。”使用计时器,你不需要暂停或延迟应用程序,直到打开PDF,你可以做任何你需要的,然后对话框会在超时后打开。“我可以使用延迟或暂停,但这不完全是我想要的。”使用计时器,您不需要暂停或延迟应用程序,直到打开PDF,您可以执行任何需要的操作,然后在超时后打开对话框。您已经找到答案了吗?我需要打开一个可以编辑的文件,并等待修改后关闭。有这个桌面功能,还有一个运行时类,它返回进程的ref,但它不会在默认应用程序中打开文件。我不知道,如何组合它们。你已经找到答案了吗?我需要打开一个可以编辑的文件,并等待修改后关闭。有这个桌面功能,还有一个运行时类,它返回进程的ref,但它不会在默认应用程序中打开文件。我不知道,如何把它们结合起来。