2016-08-01 105 views
0

我有一个程序,当出现提示时,打开一个外部编辑器(当前硬编码为崇高)。然后,用户将在编辑器中键入正在输入的内容,保存临时文件,然后关闭编辑器。当用户关闭编辑器时,我希望临时文件的内容显示在程序中。我主要关心的是创建一个条件,可以告诉编辑器何时关闭。 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(); 
     } 

谢谢你,让我知道如果你需要任何额外的信息。

回答

3

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... 
} 
+0

工作就像一个魅力。谢谢!将尽快接受答案 –