简体   繁体   中英

Open a file using Desktop(java.awt)

Am using the following code snippet to open a file,

if (Desktop.isDesktopSupported()) {
 try {
     Desktop desktop = Desktop.getDesktop();
     File myFile = new File(fileName);
     desktop.open(myFile);
     } catch (IOException ex) {}
 }

if the file i try to open is of normal .txt or .pdf any such file types i can able to open the file.

But in some cases,

在此输入图像描述

As shown above the Type of file is File , in this scenario the file is not opened.

if i manually open this File type file i get open with window and using a text editor i am able to open it.

How should i handle this in java? Please help..

Windows says the type of the file is "file" when there is no extension. This is why there is no default opener, and that is why Java doesn't open the file properly.

You can either rename the file and give it an extension, or use Java to directly execute the program you want to open the file with.

#open方法找到指定的默认应用程序来打开指定的文件但是在.file类型中没有默认的应用程序来打开文件。你只能手动指定在特定的应用程序中打开它。所以如果你设置应该打开的默认应用程序.file并且您必须设置默认应用程序并选择默认程序来打开文件而不是运行程序。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM