2011-05-16 47 views
2

我使用Java創建了一個記事本應用程序,該應用程序位於jar文件中。在Java應用程序環境中打開一個.rtx文件?

使用此功能,我創建了一個文本文件並使用文件擴展名.rtx保存。

現在我想要在Windows或任何其他平臺中右鍵單擊file.rtx,並在彈出窗口中選擇要顯示我的記事本應用程序。如果它被選中,我想打開該文件以在記事本環境中顯示其內容。

或者,雙擊該文件應導致打開我的記事本環境中的文件。

回答

0

這不是一個特別的Java問題。您需要將.rtx擴展名與您的應用相關聯。

這通常由用戶在他們的操作系統首選項中完成。根據操作系統(和應用程序類型),應用程序可能會自行設置文件關聯,但這取決於各種因素。我不確定是否可以用普通的舊JAR來完成。

有關特定於Windows的解決方案,請參閱this answer

0

這取決於您定位的操作系統。例如,如果您想嘗試使用Windwos,那麼您可以查閱本文,其中描述了JDIC,並且有示例代碼(我將在後面介紹這些代碼)可以幫助您實現這一點。

http://java.sun.com/developer/technicalArticles/J2SE/Desktop/jdic_assoc/

創建一個文件關聯:

import org.jdesktop.jdic.desktop.*; 
import org.jdesktop.jdic.filetypes.*; 

AssociationService serv = new AssociationService(); 
Association logassoc = new Association(); 

// Adds the .log type to the Association object. 

logassoc.addFileExtension("LOG"); 

// Adds an Action to the Association object that will 
// open a .log file with Windows Notepad. 

logassoc.addAction(
    new org.jdesktop.jdic.filetypes.Action(
    "open", 
    "C:\\WINDOWS\\system32\\NOTEPAD.EXE %1")); 

try { 

    // Adds the .log Association to the file types' table 
    // at the user level using an AssociationService object. 

    serv.registerUserAssociation(logassoc); 

} 
catch (java.lang.IllegalArgumentException e) { 

    // This exception will be caught if the given Association is not valid 
    // to be added to the table of file types. 

    System.err.println(e); 

} 
catch (AssociationAlreadyRegisteredException e) { 

    // This exception will be caught if the Association already 
    // exists in the table of file types. 

    System.err.println(e); 

} 
catch (RegisterFailedException e) { 

    // This exception will be caught if the Association was 
    // unable to be added to the table of file types. 

    System.err.println(e); 

} 

刪除關聯:

import org.jdesktop.jdic.desktop.*; 
import org.jdesktop.jdic.filetypes.*; 

AssociationService serv = new AssociationService(); 

// This uses an AssociationService to search the table of file 
// types for the .log extension. If the .log file is found, 
// an Association object representing the .log file type 
// will be returned. Otherwise, null is returned. 

Association logassoc = serv.getFileExtensionAssociation("LOG"); 

try { 

    // The AssociationService will remove the .log file type from 
    // the table of file types. 

    serv.unregisterUserAssociation(logassoc); 

} catch (java.lang.IllegalArgumentException e) { 

    // This exception will be caught if the given Association is not valid 
    // to be removed from the table of file types. 

    System.err.println(e); 

} catch (AssociationNotRegisteredException e) { 

    // This exception will be caught if the Association does not already 
    // exist in the table of file types. 

    System.err.println(e); 

} catch (RegisterFailedException e) { 

    // This exception will be caughtif the association was unable to be 
    // removed from the table of file types. 

    System.err.println(e); 

} 
0

啓動的應用程序。使用Java Web Start,並在JNLP啓動文件中聲明對.rtx文件類型的興趣。當用戶雙擊一個.rtx文件時,您的應用程序的主將被調用,參數爲-open path/to/the/file.rtx

這是我的demo. of the file services of the JNLP API,聲明的文件類型爲.zzz

相關問題