Java调用系统默认程序打开本地文件
2014-03-12 23:42
716 查看
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);
}
}
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调用系统默认程序打开本地文件
- Java调用系统默认程序打开本地文件
- Java调用本地相应程序打开文件
- WindowForms调用系统默认程序打开文件
- Java:调用CMD命令打开文件或系统程序
- 使用Intent调用系统其它程序打开本地各种类型的文件
- Java 通过调用系统程序打开文件或者目录的方式
- 转-Java调用本机默认程序打开各种文件(Desktop类)
- AIR程序调用本地默认应用程序打开本地文件
- AIR程序调用本地默认应用程序打开本地文件
- Java 调用系统默认程序打开
- WIN32,,打开文件没有默认程序,调用系统的默认程序选择窗口
- (4)通过调用hadoop的java api实现本地文件上传到hadoop文件系统上
- Java程序打开本地文件
- C#中调用资源管理器(Explorer.exe)打开指定文件夹 + 并选中指定文件 + 调用(系统默认的播放类)软件(如WMP)打开(播放歌曲等)文件
- 使用系统默认的程序打开文件
- android 调用系统程序打开指定文件
- java 调用系统默认 浏览器打开指定地址
- 调用Android系统安装的软件打开文件,程序停止运行
- 【已解决】C#中调用资源管理器(Explorer.exe)打开指定文件夹 + 并选中指定文件 + 调用(系统默认的播放类)软件(如WMP)打开(播放歌曲等)文件