我使用Java创建了一个记事本应用程序,该应用程序位于jar
文件中。在Java应用程序环境中打开一个.rtx文件?
使用此功能,我创建了一个文本文件并使用文件扩展名.rtx
保存。
现在我想要在Windows或任何其他平台中右键单击file.rtx
,并在弹出窗口中选择要显示我的记事本应用程序。如果它被选中,我想打开该文件以在记事本环境中显示其内容。
或者,双击该文件应导致打开我的记事本环境中的文件。
我使用Java创建了一个记事本应用程序,该应用程序位于jar
文件中。在Java应用程序环境中打开一个.rtx文件?
使用此功能,我创建了一个文本文件并使用文件扩展名.rtx
保存。
现在我想要在Windows或任何其他平台中右键单击file.rtx
,并在弹出窗口中选择要显示我的记事本应用程序。如果它被选中,我想打开该文件以在记事本环境中显示其内容。
或者,双击该文件应导致打开我的记事本环境中的文件。
这不是一个特别的Java问题。您需要将.rtx
扩展名与您的应用相关联。
这通常由用户在他们的操作系统首选项中完成。根据操作系统(和应用程序类型),应用程序可能会自行设置文件关联,但这取决于各种因素。我不确定是否可以用普通的旧JAR来完成。
有关特定于Windows的解决方案,请参阅this answer。
这取决于您定位的操作系统。例如,如果您想尝试使用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);
}
启动的应用程序。使用Java Web Start,并在JNLP启动文件中声明对.rtx
文件类型的兴趣。当用户双击一个.rtx
文件时,您的应用程序的主将被调用,参数为-open path/to/the/file.rtx
。
这是我的demo. of the file services of the JNLP API,声明的文件类型为.zzz
。