天天看點

java打開本地檔案

以下有三種方式打開

1.借助java.awt.Desktop打開

    private static void useAWTDesktop() throws IOException{

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

    }

2.借助cmd指令打開

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

    }

3. 借助本地安裝程式打開

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

    }

    }