现在的位置: 首页 > 综合 > 正文

Java代码调用操作系统可执行文件打开相应文件一行代码实现

2013年04月05日 ⁄ 综合 ⁄ 共 970字 ⁄ 字号 评论关闭

/*

*解决了含有空格目录下的错误

Runtime.getRuntime().exec("cmd /c" + "D:/1.txt");

/*

public static void openSystemExeFile() {


String strurl ="D:\t.txt";
File crtfle = new File(strurl);
if (crtfle != null && crtfle.exists()) {
int index = strurl.lastIndexOf(".");
String format = strurl.substring(index + 1, strurl.length());
if (format.equals("exe")) {
try {
Runtime.getRuntime().exec(strurl); // 执行系统的exe文件
RegRead rg = RegRead.getInstance();
String filePath2 = "\"" + strurl + "\"";
if (rg.ShellExecute(filePath2, null)) { // appName,
}
String exePath = rg.GetOnlyOpenTool(format);
if (exePath == null || exePath.equals("")) {
return;
}
return;
} catch (IOException ex) {
ex.printStackTrace();
}
} else {
// 如果是可执行.
String formats = substr(strurl).toLowerCase();
if (formats == null || "".equals(formats)) {
return;
}
if (formats.equals("exe")) {
try {
Runtime.getRuntime().exec(strurl);
return;
} catch (IOException ex) {
ex.printStackTrace();
}
} else {
try {
System.out.println(strurl);
Runtime.getRuntime().exec("cmd /c" + strurl);// 文件路径,(cmd
// /c是系统命令)
} catch (IOException e) {
e.printStackTrace();
}
return;
}
}
}
}

抱歉!评论已关闭.