我一直在使用以下代码来打开Office文档、PDF等文件,它在我的Windows机器上使用Java运行良好,除了当文件名中有多个连续空格时(例如"File[SPACE][SPACE]Test.doc")会出现问题。
我该怎么做才能让它正常工作呢?我不排斥放弃整段代码...但我不想用调用JNI的第三方库来替换它。
我该怎么做才能让它正常工作呢?我不排斥放弃整段代码...但我不想用调用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);
}
编辑: 当我运行包含错误文件时,Windows会抱怨找不到该文件。但是...当我直接从命令行运行命令时,它可以正常运行。