从Java打开文档的更好方法?

All*_*nde 2 java windows process

我一直在使用以下代码在我的Windows机器上使用Java打开Office文档,PDF等,并且它工作正常,除了某些原因,当文件名将其嵌入其中的多个连续空格如"File [SPACE] [ SPACE] Test.doc的".

我怎样才能做到这一点?我并不反对将整段代码编成罐头......但我宁愿不用一个调用JNI的第三方库替换它.

public static void openDocument(String path) throws IOException {
    // Make forward slashes backslashes (for windows)
    // Double quote any path segments with spaces in them
    path = path.replace("/", "\\").replaceAll(
            "\\\\([^\\\\\\\\\"]* [^\\\\\\\\\"]*)", "\\\\\\\"$1\"");

    String command = "C:\\Windows\\System32\\cmd.exe /c start " + path + "";

    Runtime.getRuntime().exec(command);            
}
Run Code Online (Sandbox Code Playgroud)

编辑:当我用错误的文件窗口运行它抱怨找到文件.但是......当我直接从命令行运行命令行时,它运行得很好.

Dan*_*yer 5

如果您使用的是Java 6,则只需使用java.awt.Desktopopen方法即可使用当前平台的默认应用程序启动该文件.