如何使用Java查找默认文件打开程序? [英] How to Find Out Default File Opener with Java?

查看:175
本文介绍了如何使用Java查找默认文件打开程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到Windows上给定文件的默认文件打开器,以便我可以自定义命令参数并使用默认的opener / viewer打开文件。

I need to find out default file opener for a given file on Windows so that I can customize the command arguments and open the file with the default opener/viewer.

我的实际使用场景是使用用户的默认媒体播放器打开多个多媒体文件,以便将所有文件添加到用户的播放列表中(对于可以在同一个媒体上打开多个文件的播放器)。对于Windows以外的操作系统,我使用 Desktop.open(文件文件)方法(我根本不关心为Windows以外的操作系统打开多个文件功能),我找不到任何我可以打开多个文件而不是自定义命令参数并使用Runtime类的exec()方法运行它的方法。我使用类似于此的somethig:

My real usage scenario is opening multiple multimedia files with user's default media player so that all the files will be added to user's playlist (For the players that can open multiple files on the same intance). For operating system other than Windows I use Desktop.open(File file) method (I simply does not concern opening multiple files feature for OSs other than Windows), I cannot find any method which I can open multiple files other than customizing command arguments and running it using exec() method of the Runtime class. I use somethig similar to this:

private void playItems2(List<File> fileList, String playerBinary) {
    String args = " ";
    for (File file : fileList) {
        args += "\"" + file.getAbsolutePath() + "\" ";
    }

    try {
        String command = playerBinary + args;
        Runtime rt = Runtime.getRuntime();
        Process p = rt.exec(command);
    } catch (Exception exc) {/*handle exception*/
        System.err.println("Run Player Exc:" + exc.getMessage());
    }
}

我使用用户指定的路径作为 playerBinary ,我需要的是自动检测第一项 fileList 的默认播放器并将其用作 playerBinary

I am using user specified path for the playerBinary, what I need to is automatically detecting default player for the first item of fileList and use it as playerBinary.

我还查看了Rundll32.exe和 cmd.exe / start 解决方案,但它们不能用于我的使用场景。

I have also looked at Rundll32.exe and cmd.exe /start solutions but they did not work for my usage scenario.

不应将此问题与这个

推荐答案

使用此方法调用默认开启者并享受!

Use this approach to call default opener and enjoy!

public void playItems2(...) throws Exception {
    ...
    Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler c:/mp3/myfile.mp3");
    p.waitFor();
    ...
}

这篇关于如何使用Java查找默认文件打开程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆