簡體   English   中英

Java是否有平台無關的代碼來調用默認應用程序以查看諸如PDF,DOCX,JPEG等文檔類型?

[英]Is there platform-agnostic code in Java to invoke the default app to view document types like PDF, DOCX, JPEG etc.?

我想編寫一個Java類(或方法),其職責是在任何平台上調用標准/默認應用程序,以打開和處理特定格式的文件,例如PDF,DOCX,XLSX,JPG等,並且還要知道是否提供了適合該文件格式的應用程序。

是否有一種不錯的跨平台方法?

老實說...答案並不是什么大秘密,畢竟自Java 1.6以來,所有的Desktop Class都已存在。

要使用文件的關聯應用程序(例如:帶有MS WORD的 .docx或系統與該文件關聯的任何應用程序)打開文件,則可以使用Desktop.getDesktop()。open()方法。 此方法應與Desktop.isDesktopSupported()方法相結合,以確保當前平台支持Desktop類。 這是一個演示上述桌面類方法的用法的小方法:

public void runFile (String filePath) throws IOException {
    File myFile = new File(filePath);
    //Test whether the Desktop class is supported on the current platform.
    if (Desktop.isDesktopSupported()) {
        // Open the file in its associated application:
        Desktop.getDesktop().open(myFile);
    }
    else {
        // Desktop Not Supported...
        System.err.println("runFile() method error! The Desktop Class " +
                           "is not supported on this platform!");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM