简体   繁体   English

从Java打开文件编辑器

[英]Open file editor from java

I am trying to open file in external editor from java, but when i run my source code, nothing happens. 我试图从Java在外部编辑器中打开文件,但是当我运行源代码时,什么也没发生。 I am using JRE 1.6 and my opration system is Windows 7. Here is my source code: 我正在使用JRE 1.6,操作系统是Windows7。这是我的源代码:

Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
  desktop = Desktop.getDesktop();
}

 desktop.edit(new File("D:\\Document.rtf"));

The following should also work: 以下内容也应该起作用:

Runtime.getRuntime().exec( "cmd /C D:\\Document.rtf" );

or 要么

    Runtime run = Runtime.getRuntime();
    String lcOSName = System.getProperty("os.name").toLowerCase();
    boolean MAC_OS_X = lcOSName.startsWith("mac os x");
    if (MAC_OS_X) {
        run.exec("open " + file);
    } else {
        //run.exec("cmd.exe /c start " + file); //win NT, win2000
        run.exec("rundll32 url.dll, FileProtocolHandler " + path);
    }
  • did you read API 你读过API吗

public void edit(File file) throws IOException 公共无效edit(文件文件)抛出IOException

Launches the associated editor application and opens a file for editing. 启动关联的编辑器应用程序,并打开文件进行编辑。

Parameters: file - the file to be opened for editing Throws: NullPointerException - if the specified file is null IllegalArgumentException - if the specified file doesn't exist UnsupportedOperationException - if the current platform does not support the Desktop.Action.EDIT action IOException - if the specified file has no associated editor, or the associated application fails to be launched SecurityException - if a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies read access to the file, or SecurityManager.checkWrite(java.lang.String) method denies write access to the file, or it denies the AWTPermission("showWindowWithoutWarningBanner") permission, or the calling thread is not allowed to create a subprocess See Also: AWTPermission 参数:file-要打开以进行编辑的文件抛出:NullPointerException-如果指定的文件为空IllegalArgumentException-如果指定的文件不存在UnsupportedOperationException-如果当前平台不支持Desktop.Action.EDIT操作IOException-如果指定的文件没有关联的编辑器,或者关联的应用程序无法启动SecurityException-如果存在安全管理器并且其SecurityManager.checkRead(java.lang.String)方法拒绝对该文件或SecurityManager.checkWrite(java)的读取访问。 lang.String)方法拒绝对该文件的写访问,或者拒绝AWTPermission(“ showWindowWithoutWarningBanner”)权限,或者不允许调用线程创建子进程。另请参见:AWTPermission

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM