【问题标题】:How to open and view a file(similar to that of double clicking a file) using Java如何使用Java打开和查看文件(类似于双击文件)
【发布时间】:2023-03-27 19:57:01
【问题描述】:

我想知道 Java 中的代码,这将有助于执行与在任何操作系统中双击打开文件相同的操作,从而使我们能够在用户提供文件时查看其内容文件在他/她的 PC 中的位置。任何建议都会有很大帮助,因为我需要它来完成我的申请。

【问题讨论】:

标签: java file-io platform-independent


【解决方案1】:

【讨论】:

【解决方案2】:

我使用了以下代码。在 Windows 上,如果没有程序与文件类型相关联,您将获得一个 Windows 文件打开对话框。如果它不在 Windows 上运行,它会回退到 Desktop.open(),如果系统知道文件类型,它就会工作。

public static boolean openFile(File file){
  try {
    if (System.getProperty("os.name").contains("Windows")) {
      Runtime.getRuntime().exec(
          "rundll32 SHELL32.DLL,ShellExec_RunDLL " + file.getAbsolutePath());
    } else if (Desktop.isDesktopSupported()) {
      Desktop.getDesktop().open(file);
    } else {
      return false;
    }
    return true;
  } catch (final Exception e1) {
    System.err.println("Error opening file " + file, e1);
    return false;
  }
}

【讨论】:

    【解决方案3】:

    在 Windows XP 中:

    rundll32 shell32.dll,ShellExec_RunDLL "file:///d:\download\theapp.lnk"
    

    您可以添加注册表,以便您可以在运行对话框和文件夹巡洋舰中运行 lnk 文件:

    Windows Registry Editor Version 5.00
    
    [HKEY_CLASSES_ROOT\lnkfile\shell]
    
    [HKEY_CLASSES_ROOT\lnkfile\shell\edit]
    
    [HKEY_CLASSES_ROOT\lnkfile\shell\edit\command]
    @="rundll32 shell32.dll,ShellExec_RunDLL \"file:///%1\""
    

    【讨论】:

      猜你喜欢
      • 2010-11-17
      • 1970-01-01
      • 2012-03-04
      • 2011-01-01
      • 2014-02-05
      • 1970-01-01
      • 2018-05-12
      • 2020-10-14
      • 1970-01-01
      相关资源
      最近更新 更多