java代码调用系统默认程序打开相应类型文件

来源:http://thetopofqingshan.iteye.com/blog/1500194


package Interesting;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

/**
* <pre>
* 调用系统程序打开指定文件:三种方法--@清山
* </pre>
* <hr Color="green" ></hr>
* 2011 Qingshan Group版权所有
* <hr Color="green" ></hr>
* @author thetopofqingshan
* @version 1.0.2
* @since JDK 1.5
* @date 2011-12-30
*/
public class OpenVariousFiles {

public static void main(String[] args) {
new OpenVariousFiles().run();
}

/**
* <pre>
* 测试各种方法实现的效果
* </pre>
*
*/
public void run() {
String filePath = "B:/java资料/课程标准.doc";
System.out.println(byDesktop(filePath)==true?"打开成功":"打开失败");

String application = "C:/Program Files/Kingsoft/WPS Office Personal/office6/wps.exe";
myself_application(application, filePath);

commonOpen(filePath);
}

/**
* <pre>
* 调用桌面已有程序打开指定文件
* </pre>
* @param path
* @return 输入的路径格式是否是文件/打开文件的情况
*
*/
public boolean byDesktop(String path){
/**根据路径创建文件对象*/
File file = new File(path);
System.out.println(file.exists());
/**验证文件对象是否是文件*/
if(file.isFile()){
/**得到java.awt.Desktop对象 */
Desktop desktop = Desktop.getDesktop();
try {
/**打开文件*/
desktop.open(file);
} catch (IOException e) {
System.out.println(e);
}
return true;
}else{
return false;
}
}

/**
* <pre>
* 调用系统指定应用程序打开指定文件
* </pre>
* @param applicationPath 程序名称
* @param filePath 文件路径路径
* @return 输入的路径格式是否是文件/打开文件的情况
*
*/
public boolean myself_application(String applicationPath, String filePath){
try {
/**开启本地系统相应进程来打开文件 注意:两个参数的间的空格*/
Runtime.getRuntime().exec(applicationPath+" "+filePath);
return true;
} catch (IOException e) {
System.out.println(e);
return false;
}
}

/**
* <pre>
* 通用:只要系统有打开相应文件的程序
* </pre>
* @param filePath 文件路径路径==>注:如果打开目录或文件名含有空格, 将找不到,有哪位朋友已解决,请留言
* @return 输入的路径格式是否是文件/打开文件的情况
*/
public boolean commonOpen(String filePath){
try {
/**开启本地系统相应进程来打开文件 注意:两个参数的间的空格*/
Runtime.getRuntime().exec("cmd /c start "+filePath);
return true;
} catch (IOException e) {
System.out.println(e);
return false;
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值