您的位置:首页 > 编程语言 > Java开发

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

2013-01-05 20:02 826 查看
/*

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

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;

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐