经过百度,千篇一律的内容,如下:
1.java自带的API
打开文件:
File file=new File("c://test.pdf");
java.awt.Desktop.getDesktop().open(file);
打开目录:
File file=new File("c://test.pdf");
java.awt.Desktop.getDesktop().open(file.getParentFile());
在jdk1.6上测试失败,一执行这段代码,应用程序直接卡死,不知道是什么原因。
后又经过百度。
通过java直接执行cmd命令得以实现。
1try { 2String path="c:\\abc"; 3 Runtime.getRuntime().exec("explorer.exe /select, "+path); 4 } catch (Exception e1) { 5 JOptionPane.showMessageDialog(null, "打开目录失败!", "错误提示",JOptionPane.ERROR_MESSAGE); 6 7 }