Java调用系统默认程序打开本地文件

本文介绍了一种使用Java程序来调用系统默认应用程序打开本地文件的方法。提供了三种不同的实现方式:通过ProcessBuilder、AWT Desktop和Runtime Exec。这些方法能够有效地处理含有空格的路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. package com.jadyer.util;  
  2.   
  3. import java.awt.Desktop;  
  4. import java.io.File;  
  5. import java.io.IOException;  
  6. import java.util.ArrayList;  
  7. import java.util.List;  
  8.   
  9. /** 
  10.  * Java调用系统默认程序打开本地文件 
  11.  * @see 这里提供了三种打开方式,大家可自行选取 
  12.  * @create Apr 23, 2013 1:00:37 AM 
  13.  * @author 玄玉<http://blog.youkuaiyun.com/jadyer> 
  14.  */  
  15. public class OpenLocalFile {  
  16.     public static void main(String[] args) throws IOException {  
  17.         useProcessBuilder();  
  18.         useAWTDesktop();  
  19.         useRuntimeExec();  
  20.     }  
  21.       
  22.       
  23.     /** 
  24.      * 借助java.lang.ProcessBuilder打开 
  25.      */  
  26.     private static void useProcessBuilder() throws IOException{  
  27.         //new ProcessBuilder("notepad.exe", "C:/Users/Jadyer/Desktop/test file/readme.txt").start();  
  28.         List<String> commands = new ArrayList<String>();  
  29.         commands.add("D:/Program Files/WPS/9.1.0.4047/office6/wps.exe");  
  30.         commands.add("C:/Users/Jadyer/Desktop/test file/myResume.doc");  
  31.         new ProcessBuilder(commands).start();  
  32.     }  
  33.       
  34.       
  35.     /** 
  36.      * 借助java.awt.Desktop打开 
  37.      * @see 打开的目录或文件名中允许包含空格 
  38.      */  
  39.     private static void useAWTDesktop() throws IOException{  
  40.         Desktop.getDesktop().open(new File("D:/my local/测试用例.xls"));  
  41.     }  
  42.       
  43.       
  44.     /** 
  45.      * 借助java.lang.Runtime打开 
  46.      * @see WPS文字--------Runtime.getRuntime().exec("cmd /c start wps") 
  47.      * @see WPS表格--------Runtime.getRuntime().exec("cmd /c start et") 
  48.      * @see WPS演示--------Runtime.getRuntime().exec("cmd /c start wpp") 
  49.      * @see Office Word---Runtime.getRuntime().exec("cmd /c start winword") 
  50.      * @see Office Excel--Runtime.getRuntime().exec("cmd /c start excel") 
  51.      */  
  52.     private static void useRuntimeExec() throws IOException{  
  53.         /* 
  54.          * 若打开的目录或文件名中不包含空格,就用下面的方式 
  55.          */  
  56.         Runtime.getRuntime().exec("cmd /c start D:/mylocal/测试用例.xls");  
  57.         /* 
  58.          * (可以'运行'或'Win+R',然后输入'cmd /?'查看帮助信息) 
  59.          */  
  60.         Runtime.getRuntime().exec(new String[]{"cmd.exe""/c""D:/my local/测试用例.xls"});  
  61.         /* 
  62.          * 借助本地安装程序打开 
  63.          * 若打开的目录或文件名中包含空格,它就无能为力了..不过本地程序的安装目录允许含空格 
  64.          */  
  65.         String etCommand = "D:/Program Files/WPS/8.1.0.3526/office6/et.exe";  
  66.         String filePath = "D:/mylocal/测试用例.xls";  
  67.         Runtime.getRuntime().exec(etCommand + " " + filePath);  
  68.     }  
  69. }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值