java 打开本地文件

需求:点击帮助按钮,打开项目中的帮助文档

搜索了网上的信息,进行整理,参考资源有:

  Java调用系统默认程序打开本地文件http://www.2cto.com/kf/201304/205831.html

 

/**
     * 借助java.awt.Desktop打开
     * @see 打开的目录或文件名中允许包含空格

    *  @see 因需要用到java.awt包,所以,跨平台性差些
     */

    Desktop.getDesktop().open(new File("D:/my local/测试用例.xls")); 


/**
     * 借助cmd命令打开
     * @see WPS文字--------Runtime.getRuntime().exec("cmd /c start wps")
     * @see WPS表格--------Runtime.getRuntime().exec("cmd /c start et")
     * @see WPS演示--------Runtime.getRuntime().exec("cmd /c start wpp")
     * @see Office Word---Runtime.getRuntime().exec("cmd /c start winword")
     * @see Office Excel--Runtime.getRuntime().exec("cmd /c start excel")
     */ 

     //若打开的目录或文件名中不包含空格,就用下面的方式  
     Runtime.getRuntime().exec("cmd /c start D:/mylocal/测试用例.xls");  
     //实验过了,目录和文件名可以含空格
     Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", "D:/my local/测试用例.xls"}); 


扫描二维码关注公众号,回复: 5737406 查看本文章

 /**
     * 借助本地安装程序打开
     * @see 若打开的目录或文件名中包含空格,它就无能为力了..不过本地程序的安装目录允许含空格
     */

       String etCommand = "D:/Program Files/WPS/8.1.0.3526/office6/et.exe"; 
        String filePath = "D:/mylocal/测试用例.xls"; 
        Runtime.getRuntime().exec(etCommand + " " + filePath); 


我选的第二种方案



猜你喜欢

转载自blog.csdn.net/u014165193/article/details/39204655