pra*_*een 0 java apache-commons properties-file
我目前正在使用Apache commons配置库来编写和读取文件中的数据.我能够将键值对colors = hello保存到user..properties文件中,但是当我尝试读取该值时,获取以下异常.
Exception in thread "main" java.lang.IllegalArgumentException: 'hello' does not contain an equals sign
at org.apache.commons.configuration.AbstractConfiguration.getProperties(AbstractConfiguration.java:625)
at org.apache.commons.configuration.AbstractConfiguration.getProperties(AbstractConfiguration.java:579)
at com.code.prep.CommonsMain.readProperties(CommonsMain.java:21)
at com.code.prep.CommonsMain.main(CommonsMain.java:12)
Run Code Online (Sandbox Code Playgroud)
代码如下
package com.code.prep;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
public class CommonsMain {
public static void main(String[] args) {
CommonsMain main = new CommonsMain();
main.readProperties();
// main.writeProperties();
}
public void readProperties(){
PropertiesConfiguration config = new PropertiesConfiguration();
try {
config.load("user.properties");
System.out.println(config.getProperties("colors"));
} catch (ConfigurationException e) {
e.printStackTrace();
}
}
public void writeProperties(){
PropertiesConfiguration config = new PropertiesConfiguration();
try {
config.load("user.properties");
config.setProperty("colors", "hello");
config.save("user.properties");
} catch (ConfigurationException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
类路径中的Jars是:
commons-configuration-1.9.jar commons-lang-2.4.jar commons-logging-1.1.1.jar
user.properties包含
colors = hello user = thejavamonk
你不应该使用
config.getProperties("colors")
Run Code Online (Sandbox Code Playgroud)
但
config.getProperty("colors")
Run Code Online (Sandbox Code Playgroud)
"getProperties(code)"正在查找表单的user.properties文件中的(多个)行:
code key=val
Run Code Online (Sandbox Code Playgroud)
所以它期待你的代码,因为它具有如下行:
colors foreground=black
colors background=white
Run Code Online (Sandbox Code Playgroud)
等等
| 归档时间: |
|
| 查看次数: |
1151 次 |
| 最近记录: |