在Java中,如何在单击按钮时为用户打开一个单独的文件夹(例如c :),例如,当我们下载文件并想要知道它保存在哪里。目的是节省用户打开浏览器并在磁盘上找到文件的时间。谢谢(下图是firefox的示例)
我得到了答案:这是在Windows 7中对我有用的东西:
File foler = new File("C:\\"); // path to the directory to be opened
Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}
try {
desktop.open(foler);
} catch (IOException e) {
}
感谢@AlexS
我假设您有一个文件。借助java.awt.Desktop,您可以使用以下代码:
public static void openContaiingFolder(File file) {
String absoluteFilePath = file.getAbsolutePath();
File folder = new File(absoluteFilePath.substring(0, absoluteFilePath.lastIndexOf(File.separator)));
openFolder(folder);
}
public static void openFolder(File folder) {
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(folder);
}
}
请注意,如果使用没有目录的文件调用此文件,至少Windows会尝试使用该文件类型的默认程序打开该文件。
但是我不知道在哪些平台上支持此功能。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句