我试图在我的jar中更新我的.properties文件,但是没有成功。在尝试以上千种不同的方式进行此操作之后,我在这里得到了以下示例:
final AbstractFileConfiguration prop = new PropertiesConfiguration("application.properties");
System.out.println(prop.getProperty(property));
if (prop.containsKey(property)) {
prop.setProperty(property, value);
prop.save();
}这对我来说是个例外:
org.apache.commons.configuration.ConfigurationException: Could not save to URL jar:file:/home/pedepano/myjarapp.jar!/application.properties
at org.apache.commons.configuration.AbstractFileConfiguration.save(AbstractFileConfiguration.java:464)
at org.apache.commons.configuration.AbstractFileConfiguration.save(AbstractFileConfiguration.java:377)
at com.library.core.Configs.updateSimpleProperty(Configs.java:151)
at com.print.model.business.caixa.cnab240.listmodel.AbstractCnab240ListModel.mouseClicked(AbstractCnab240ListModel.java:111)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
at java.awt.Component.processMouseEvent(Component.java:6528)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)有人说这不可能在运行时更新.properties文件,也有人说是这样。我实在太浪费时间去找答案了.这有可能吗?如果是,怎么做?如果没有,我能做些什么呢?
非常感谢!
发布于 2015-08-27 17:07:07
据我所知,您正在尝试将已编译到您的.properties文件中的.jar文件写回,但这是不可能的;但是,您可以将该属性文件写入外部源。
File f = new File("my.properties");
OutputStream out = new FileOutputStream( f );
props.store(f, "a comment");这将与您的.jar文件一起创建该文件。
如果这是您选择的路径,则可能首先尝试从外部文件加载,如果找不到外部文件,则默认加载为默认路径。
发布于 2017-07-26 13:29:37
Properties properties = new Properties();
try {
String propertiesFilePath = ("your file path");
FileInputStream fis = new FileInputStream(propertiesFilePath);
properties.load(fis);
properties.setProperty("key","value");
FileOutputStream fos = new FileOutputStream(propertiesFilePath);
properties.store(fos,null);
System.out.println("SUCCESS");
}
catch(Exception e) {
System.out.println("FAILED");
}https://stackoverflow.com/questions/32255428
复制相似问题