用java打開一個本地文件


以下有三種方式打開

/**

    * 借助java.awt.Desktop打開

    * @see 打開的目錄或文件名中允許包含空格

    */

    private static void useAWTDesktop() throws IOException{

    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")

    */

    private static void useCMDCommand() throws IOException{

    //若打開的目錄或文件名中不包含空格,就用下面的方式

    //Runtime.getRuntime().exec("cmd /c start D:/mylocal/測試用例.xls");

    //(可以'運行'或'Win+R',然后輸入'cmd /?'查看幫助信息)

    Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", "D:/my local/測試用例.xls"});

    }

    /**

    * 借助本地安裝程序打開

    * @see 若打開的目錄或文件名中包含空格,它就無能為力了..不過本地程序的安裝目錄允許含空格

    */

    private static void useLocalCommand() throws IOException{

    String etCommand = "D:/Program Files/WPS/8.1.0.3526/office6/et.exe";

    String filePath = "D:/mylocal/測試用例.xls";

    Runtime.getRuntime().exec(etCommand + " " + filePath);

    }

    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM