Java的:按钮点击打开文件夹 [英] java: Open folder on button click
问题描述
在Java中,我们如何可以打开的单独的文件夹(如C :)用户上的一个按钮,如喜欢的方式的点击定位磁盘文件或打开包含文件夹当我们下载一个文件,我们想知道在哪里保存它呢。我们的目标是,以节省用户的时间去打开浏览器并找到磁盘上的文件。
谢谢(以下图片是什么Firefox没有一个例子)
In java, how can we open a separate folder (e.g. c:) for user on click of a button, e.g like the way " locate this file on disk" or "open containing folder" does when we download a file and we want to know where it was saved. The goal is to save user's time to open a browser and locate the file on disk. Thanks ( image below is an example from what firefox does)
我得到了答案:
下面是我在Windows 7中的工作是什么:
I got the answer: Here is what worked for me in 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
Thanks to @AlexS
推荐答案
我假设你有一个文件。随着的java.awt.Desktop你可以使用这样的:
I assume you have a file. With java.awt.Desktop you can use something like this:
File myfile = new File("/tmp/test.txt");
String path = myFile.getAbsolutePath();
File dir = new File(path.substring(0, path.lastIndexOf(File.separator)));
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(dir);
}
但我不知道哪些该平台支持。
But I don't know on which platforms this is supported.
这篇关于Java的:按钮点击打开文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!