1. 程式人生 > >java語言程式設計實現開啟exe可執行檔案以及txt,word等檔案功能

java語言程式設計實現開啟exe可執行檔案以及txt,word等檔案功能

java語言程式設計實現,開啟exe可執行檔案及txt、word等檔案功能,程式碼如下:
public class OpenSpecialFile{
public static void main(final String[] args) throws IOException {  
    openWindowsExe();  
    openExe();  
    openFile();  
}  
  
// 用 Java 呼叫windows系統的exe檔案,比如notepad,calc之類  
public static void openWindowsExe() {  
    final Runtime runtime = Runtime.getRuntime();  
    Process process = null;  
    try {  
        final String command = "notepad";// 記事本  
        process = runtime.exec(command);  
    } catch (final Exception e) {  
        System.out.println("Error win exec!");  
    }  
}  
  
// 呼叫其他的可執行檔案,例如:自己製作的exe,或是 下載 安裝的軟體.  
public static void openExe() {  
    final Runtime runtime = Runtime.getRuntime();  
    Process process = null;  
  
    try {  
        process = runtime.exec("C:\\Program Files\\Notepad++\\notepad++.exe");  
  
    } catch (final Exception e) {  
        System.out.println("Error exec!");  
    }  
}  
  
// 開啟其他任意格式的檔案,比如txt,word等  
public static void openFile() {  
    final Runtime runtime = Runtime.getRuntime();  
    Process process = null;//  
    final String cmd = "rundll32 url.dll FileProtocolHandler file://F:\\ECT專案資料\\建立EMF工程.txt";  
    try {  
        process = runtime.exec(cmd);  
    } catch (final Exception e) {  
        System.out.println("Error exec!");  
    }  
}  

}