使用Java 语言,如何打开文件夹和浏览文件呢?
打开文件夹或打开目录
1. /***
2. *
3. * @param folder
4. * : directory
5. */
6. public static void
7. new
8. if
9. return;
10. }
11. null;
12. try
13. runtime = Runtime.getRuntime();
14. if
15. // System.out.println("is linux");
16. "nautilus "
17. else
18. "cmd /c start explorer "
19. }
20. catch
21. ex.printStackTrace();
22. finally
23. if (null
24. runtime.runFinalization();
25. }
26. }
27. }
浏览文件:
1. /***
2. *
3. * @param filePath
4. * : only regular file
5. */
6. public static void
7. new
8. if
9. return;
10. }
11. null;
12. try
13. runtime = Runtime.getRuntime();
14. if
15. // System.out.println("is linux");
16. "nautilus "
17. else
18. "cmd /c start explorer /select,/e, "
19. }
20. catch
21. ex.printStackTrace();
22. finally
23. if (null
24. runtime.runFinalization();
25. }
26. }
27. }
应用:
1. openFileButton.addActionListener(new
2. public void
3. FileUtils.open_file(sourceTF.getText());
4. }
5. });
6.
7. openFolderButton = new JButton("打开文件夹");
8. new
9. public void
10. //打开文件夹
11. FileUtils.open_directory(targetTF.getText());
12. }
13. });