【问题标题】:Open a File in default File explorer and highlight it using JavaFX or plain Java在默认文件资源管理器中打开一个文件并使用 JavaFX 或纯 Java 突出显示它
【发布时间】:2017-04-19 05:42:18
【问题描述】:

我希望按照标题所说的去做。


部分解决方案:

例如在Windows 中,您可以使用下面的代码在默认资源管理器中打开一个文件并突出显示它。

(although it needs modification for files containing spaces):

/**
 * Opens the file with the System default file explorer.
 *
 * @param path the path
 */
public static void openFileLocation(String path) {
    if (InfoTool.osName.toLowerCase().contains("win")) {
        try {
            Runtime.getRuntime().exec("explorer.exe /select," + path);
        } catch (IOException ex) {
            Main.logger.log(Level.WARNING, ex.getMessage(), ex);
        }
    }
}

有用的链接:

相似但no way dublicates或未回答的链接:

How to use java code to open Windows file explorer and highlight the specified file?

Open a folder in explorer using Java

How can I open the default system browser from a java fx application?


更多解释:

  • 有没有办法使用 JavaFX 做到这一点?

      If not at least i need a link or some way to make the app system  
       independence.I mean i don't know the default explorer for every OS     
       that the application is going to work , i need a link or help doing that.
    
  • 我是否需要编写大量代码才能做到这一点?

  • 有没有图书馆可以做到这一点?

  • Java9 支持吗?


最后:

很奇怪,对于如此常见的事情,我找不到答案和库。


在 Windows 10 中突出显示或选择的示例:

【问题讨论】:

  • 我已准备好为那些找到答案的勇敢者提供赏金;)
  • Example of highlighted or selected in Windows 10: - 是的,Java 无法访问在 Windows 环境中运行的应用程序的单个 API。我确定为此您需要一种较低级别的语言。
  • @Sedrick Jefferson 随着 JavaFX 的不断发展,我认为它可能会通过 JNI 调用本机系统来提供这样的功能或库。实际上,如果出现这样的功能,那将是惊人的。包括第三方库在内的任何解决方案都被高度接受。可以看到 Swing 有桌面 API。
  • 这个问题没有人回答真的很可惜。我也有同样的需求。
  • 签名。差异机有差异文件浏览器,不知道如何适配更多操作系统。

标签: java javafx io explorer


【解决方案1】:

从 Java 9 开始,可以使用新方法 browseFileDirectory,因此您的方法会声明:

import java.awt.Desktop;
import java.io.File;
...

/**
 * Opens the file with the System default file explorer.
 *
 * @param path the path
 */
public static void openFileLocation(String path) {
    Desktop.getDesktop().browseFileDirectory(new File(path));
}

有关更多信息,请参阅 javadoc: https://docs.oracle.com/javase/10/docs/api/java/awt/Desktop.html#browseFileDirectory(java.io.File)

【讨论】:

  • 厉害了,就一个问题,在资源管理器中也选择了文件?
  • oracle 文档说是。我已经在 windows7 上测试过并且可以工作。在我的 manjaro linux i3 社区版中由于平台不支持该操作而引发异常,但我认为在 KDE 或 Gnome 上应该可以正常工作。
  • 完美我会做测试:)
  • 刚刚在 Windows 10 上测试过 .... java.lang.UnsupportedOperationException: The BROWSE_FILE_DIR action is not supported on the current platform! 我就像 whatttttt ...使用 Java 9.0.4
  • JDK-8233994。有意不支持 Windows 10。不知道为什么。也许它只是被遗忘了。
【解决方案2】:

以下是部分答案,向您展示如何打开所需的系统文件夹,而不是如何突出显示特定文件,因为我不相信可以突出显示系统文件夹中的文件,因为这可能是Java 无法访问的系统操作系统功能。

这是用 Javafx 代码编写的

在您的 Main 类中为 Hostservices 创建一个变量。请注意,“yourFileLocation”是文件所在文件夹的地址,而 SettsBtn 是存在于某处的按钮,用户单击该按钮即可执行代码:

public class Main extends Application{

    static HostServices Host; //<-- sort of a global variable

    //some code here to make your GUI

    public Main() {
        //more code here to initialize things
    }

    public void start(Stage primaryStage) throws Exception {
        //some code here to set the stage

        //This code here opens the file explorer
        SettsBtn.setOnMouseClicked(e-> {
            Path partPath = Paths.get("yourFileLocation");
            Host = getHostServices();
            Host.showDocument(partPath.toUri().toString());
        });
    }
}

请注意,您可以通过将文件位置和文件名及其扩展名作为字符串来直接打开文件,例如:

Path partPath = Paths.get("yourFileLocation"+"\\"+"yourFileName.ext");

【讨论】:

  • 在 Chrome 中显示:D
【解决方案3】:

窗户

Runtime.getRuntime().exec("explorer /select, <file path>")

MacOS

Runtime.getRuntime().exec("open -R <file path>");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-13
    • 1970-01-01
    • 1970-01-01
    • 2012-05-05
    相关资源
    最近更新 更多