我有一个程序,当提示时,打开一个外部编辑器(当前硬编码为 sublime)。然后,用户将在编辑器中键入他们正在键入的任何内容,保存临时文件,然后关闭编辑器。当用户关闭编辑器时,我希望临时文件的内容显示在程序中。我主要关心的是创建一个条件来判断编辑器何时关闭。 WindowListener 可以用于引用正在启动的外部程序吗?这是到目前为止我的代码:(注意:由于桌面和我当前版本的 Gnome 的兼容性问题,我使用运行时。这只能在 Linux 上运行。)
private CachedTextInfo cti;
private File temp = File.createTempFile("tempfile",".tmp");
try{
theText.setText(cti.initialText);
String currentText = theText.getText();
BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
bw.write(currentText);
bw.close();
Runtime.getRuntime().exec("subl "+ temp.getAbsolutePath());
//When editor closes, display tmp contents
}catch(IOException e) {
e.printStackTrace();
}
谢谢您,如果您需要任何其他信息,请告诉我。
最佳答案
Runtime.exec()
返回一个 Process
实例,该实例具有 waitFor()
方法。
所以你可以这样做
Process p = Runtime.getRuntime().exec("subl "+ temp.getAbsolutePath());
try {
p.waitFor();
// display tmp contents...
} catch (InterruptedException exc) {
// thread was interrupted waiting for process to complete...
}
关于java - 外部程序关闭时的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38705848/