java - 在默认文件资源管理器中打开文件并使用 JavaFX 或纯 Java 突出显示它

标签 java javafx io explorer

我想按照标题所说的去做。


部分解决方案:

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

(虽然它需要修改包含空格的文件):

/**
 * 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);
        }
    }
}

有用的链接:

相似但无法重复或未回答的链接:

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 中突出显示或选择的示例:

enter image description here

最佳答案

window

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

Linux

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

MacOS

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

关于java - 在默认文件资源管理器中打开文件并使用 JavaFX 或纯 Java 突出显示它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40963947/

相关文章:

java - 对于这个简单的层次结构 Java 问题,我的最佳方法是什么?

java - 在另一个过滤器中调用 servlet 过滤器包装器

c++ - 如何在 C++ 中对文件系统进行非并发打印?

c - 优化 C 代码中的 I/O(输出)+ 循环

JavaFX css 样式不起作用 - 未知属性 + 未找到资源

JAVA 写入具有特定偏移量的文件

java - firebase 访问 token 是否自动刷新?

java - 有标准的 Facebook Java Api 吗?

JavaFX FXML Controller - 构造函数与初始化方法

JavaFX:setOnDragDropped 上的 ImageView 未触发