java打开word,ppt,excel等文件
public static void run(String path_file){
try {
File file = new File(path_file);
File dir = file.getParentFile();
String filename = file.getName();
String ext = FileUtil.getExtension(filename);
String name = FileUtil.getName(filename);
name = RegExpUtil.filterCommandLineSpecialChar(name);
String command = "cmd /c start "+name+ext;
Process process = Runtime.getRuntime().exec(command,null,dir);
int v = process.waitFor();
System.out.println(v);
} catch (Exception e) {
// e.printStackTrace();
System.out.println(e.getMessage());
}
}
文件名处理
public static String filterCommandLineSpecialChar(String s){
String regexp = "[\\pP\\pZ\\pS]+";
Pattern pattern = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(s);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
String p = matcher.group();
matcher.appendReplacement(sb, "\""+p+"\"");
}
matcher.appendTail(sb);
return sb.toString();
}