Java調用系統預設程式打開本地檔案
- package com.jadyer.util;
- import java.awt.Desktop;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- public class OpenLocalFile {
- public static void main(String[] args) throws IOException {
- useProcessBuilder();
- useAWTDesktop();
- useRuntimeExec();
- }
- private static void useProcessBuilder() throws IOException{
- //new ProcessBuilder("notepad.exe", "C:/Users/Jadyer/Desktop/test file/readme.txt").start();
- List<String> commands = new ArrayList<String>();
- commands.add("D:/Program Files/WPS/9.1.0.4047/office6/wps.exe");
- commands.add("C:/Users/Jadyer/Desktop/test file/myResume.doc");
- new ProcessBuilder(commands).start();
- }
- private static void useAWTDesktop() throws IOException{
- Desktop.getDesktop().open(new File("D:/my local/測試用例.xls"));
- }
- private static void useRuntimeExec() throws IOException{
- Runtime.getRuntime().exec("cmd /c start D:/mylocal/測試用例.xls");
- Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", "D:/my local/測試用例.xls"});
- String etCommand = "D:/Program Files/WPS/8.1.0.3526/office6/et.exe";
- String filePath = "D:/mylocal/測試用例.xls";
- Runtime.getRuntime().exec(etCommand + " " + filePath);
- }
- }
100) ? false:true" x-data="topBtn" @click="scrolltoTop" x-cloak>