天天看點

Java調用系統預設程式打開本地檔案

  1. package com.jadyer.util;  
  2. import java.awt.Desktop;  
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7. public class OpenLocalFile {  
  8.     public static void main(String[] args) throws IOException {  
  9.         useProcessBuilder();  
  10.         useAWTDesktop();  
  11.         useRuntimeExec();  
  12.     }  
  13.     private static void useProcessBuilder() throws IOException{  
  14.         //new ProcessBuilder("notepad.exe", "C:/Users/Jadyer/Desktop/test file/readme.txt").start();  
  15.         List<String> commands = new ArrayList<String>();  
  16.         commands.add("D:/Program Files/WPS/9.1.0.4047/office6/wps.exe");  
  17.         commands.add("C:/Users/Jadyer/Desktop/test file/myResume.doc");  
  18.         new ProcessBuilder(commands).start();  
  19.     }  
  20.     private static void useAWTDesktop() throws IOException{  
  21.         Desktop.getDesktop().open(new File("D:/my local/測試用例.xls"));  
  22.     }  
  23.     private static void useRuntimeExec() throws IOException{  
  24.         Runtime.getRuntime().exec("cmd /c start D:/mylocal/測試用例.xls");  
  25.         Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", "D:/my local/測試用例.xls"});  
  26.         String etCommand = "D:/Program Files/WPS/8.1.0.3526/office6/et.exe";  
  27.         String filePath = "D:/mylocal/測試用例.xls";  
  28.         Runtime.getRuntime().exec(etCommand + " " + filePath);  
  29.     }  
  30. }