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

时间:2016-12-04 21:35:22

标签: java javafx io explorer

我想做标题所说的。

部分解决方案:

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

enter image description here

3 个答案:

答案 0 :(得分:2)

好的,我知道可能要晚了,但是我已经为您找到了答案。 由于Java 9可以使用新方法browserFileDirectory,因此您的方法应为:

     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)

希望对您有帮助。

答案 1 :(得分:1)

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

这是用Javafx代码编写的

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

onItemTap: function (args) {
      console.log('Item with index: ' + args.index + ' tapped');
      args.object.backgroundColor = "#3489db";
    },

请注意,您可以通过在文件位置和扩展名的文件名中输入字符串来直接打开文件,例如:

    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());
            });
       }
     }

答案 2 :(得分:1)

Windows
i <- match(data.2$texts, data.1$texts) i <- sort(i) data.1$code_2 <- 0L data.1$code_2[i] <- data.2$code[seq_along(i)] data.1 # texts code code_2 #1 This is a sentence 1 1 #2 This is another sentence 1 0 #3 This is not a sentence 0 1 #4 Yet another sentence 1 0

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