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

参考地址 http://blog.csdn.net/jadyer/article/details/8840339

package com.jadyer.util;  

import java.awt.Desktop;  
import java.io.File;  
import java.io.IOException;  
import java.util.ArrayList;  
import java.util.List;  

/** 
 * Java调用系统默认程序打开本地文件 
 * @see 这里提供了三种打开方式,大家可自行选取 
 * @create Apr 23, 2013 1:00:37 AM 
 * @author 玄玉<http://blog.csdn.net/jadyer> 
 */  
public class OpenLocalFile {  
    public static void main(String[] args) throws IOException {  
        useProcessBuilder();  
        useAWTDesktop();  
        useRuntimeExec();  
    }  


    /** 
     * 借助java.lang.ProcessBuilder打开 
     */  
    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();  
    }  


    /** 
     * 借助java.awt.Desktop打开 
     * @see 打开的目录或文件名中允许包含空格 
     */  
    private static void useAWTDesktop() throws IOException{  
        Desktop.getDesktop().open(new File("D:/my local/测试用例.xls"));  
    }  


    /** 
     * 借助java.lang.Runtime打开 
     * @see WPS文字--------Runtime.getRuntime().exec("cmd /c start wps") 
     * @see WPS表格--------Runtime.getRuntime().exec("cmd /c start et") 
     * @see WPS演示--------Runtime.getRuntime().exec("cmd /c start wpp") 
     * @see Office Word---Runtime.getRuntime().exec("cmd /c start winword") 
     * @see Office Excel--Runtime.getRuntime().exec("cmd /c start excel") 
     */  
    private static void useRuntimeExec() throws IOException{  
        /* 
         * 若打开的目录或文件名中不包含空格,就用下面的方式 
         */  
        Runtime.getRuntime().exec("cmd /c start D:/mylocal/测试用例.xls");  
        /* 
         * (可以'运行'或'Win+R',然后输入'cmd /?'查看帮助信息) 
         */  
        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);  
    }  
} 

java如何调用linux下的类似cmd命令
参考地址 http://blog.sina.com.cn/s/blog_4d14c288010124b6.html
Processprocess=Runtime.getRuntime().exec(“shstartup.sh &”);
InputStream inputStream = process.getInputStream(); byte[] bs = newbyte[1024];
inputStream.read(bs);
inputStream.close();

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值