简体   繁体   English

让用户选择应用程序以打开文件

[英]Let user choose the application to open the file

I need my java code to open a file based upon the default application. 我需要我的Java代码才能根据默认应用程序打开文件。 Thanks to How to open user system preferred editor for given file? 感谢如何打开给定文件的用户系统首选编辑器? which suggests a quality method to do it by 这表明通过

Runtime.getRuntime().exec("RUNDLL32.EXE SHELL32.DLL,OpenAs_RunDLL "+file);

But the issue is that, once I select the application to open it up, it doesn't open the file. 但是问题是,一旦我选择了要打开的应用程序,它就不会打开文件。 I do not know the reason for it. 我不知道原因。

Thanks 谢谢

EDIT: 编辑:

Desktop.getDesktop().open(file);

This opens in default application. 这将在默认应用程序中打开。 I want the user to choose the application to open it up 我希望用户选择要打开的应用程序

Use : 采用 :

Desktop.getDesktop().open(file);

http://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html#open(java.io.File ) http://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html#open(java.io.File

EDIT: 编辑:

Here is the code to make your command work: 这是使您的命令起作用的代码:

import java.io.File;
import java.io.IOException;

public class TestExec {

    public static void main(String[] args) throws IOException, InterruptedException {
        File file = new File("d:/Clipboard1.png");
        ProcessBuilder builder = new ProcessBuilder("RUNDLL32.EXE", "SHELL32.DLL,OpenAs_RunDLL", file.getAbsolutePath());
        builder.redirectErrorStream();
        builder.redirectOutput();
        Process process = builder.start();
        process.waitFor();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 让用户在 Web 应用程序中保存文件时选择文件类型 - Let user choose file type while saving a file in Web application Eclipse:如何提示用户选择打开文件的应用程序? - Eclipse: How to prompt user to choose an application to open a file with? 让用户选择一个热键然后保存的最佳方法? - Best way to let the user choose a hotkey and then save it? 如何让用户停止Java应用程序 - How to let the user stop a java application Android Java filepicker选择应用程序以选择文件 - Android Java filepicker choose application to pick file 安装Open JDK和SunJDK并在启动应用程序时选择 - Install Open JDK and SunJDK and choose while launching an application 如何编写代码让用户选择什么都不选择或选择时间? - How can I write the code to let the User to choose nothing or pick a time? 如何让用户从联系人中选择一个电话号码,然后获取电话号码,给定姓名和姓氏 - How to let the user choose a phone number from contacts and then get the phone number, the given name and the family name 使用Java应用程序打开文件 - Open a file with a java application GUI 应用程序,允许用户选择绘图的形状和颜色 - GUI Application that allows the user to choose the shape and color of a drawing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM