java語言編程實現,打開exe可執行文件及txt、word等文件功能(指定/默認exe)

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!");  
    }  
}  
 
}
//第一種方式(指定程序)
Runtime.getRuntime().exec("D:\\Program Files\\Notepad++\\notepad++.exe"+" "+filePath);
//第二種(默認程序) 
Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", filePath});
//第三種(默認程序)
Runtime.getRuntime().exec("rundll32 url.dll FileProtocolHandler"+" "+filePath);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章