Java_swing_打开文件目录

经过百度,千篇一律的内容,如下:

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命令得以实现。
 
try {
String path="c:\\abc";
                    Runtime.getRuntime().exec("explorer.exe /select, "+path);
                } catch (Exception e1) {
                    JOptionPane.showMessageDialog(null, "打开目录失败!", "错误提示",JOptionPane.ERROR_MESSAGE);
                     
                }
 

猜你喜欢

转载自www.cnblogs.com/ydemo/p/10450417.html