我不确定如何创建超过 2 个属性,我使用了 setProperty() 方法,当我放置超过 2 个属性时,NetBeans 抛出一个语法错误,指出 setProperty() 方法只能有 2 个属性。这是我到目前为止的代码:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class MyOwnProject {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
FileInputStream propFile = null;
Properties p = null;
// set up new properties object
// from file "myProperties.txt"
try {
propFile = new FileInputStream(
"myProperties.txt");
p = new Properties(System.getProperties());
p.load(propFile);
} catch (IOException e) {
System.out.println(e);
}
// set a property through setProperty() method
p.setProperty("mykey20", "mykey30", "mykey40");
// set the system properties
System.setProperties(p);
// display new properties
System.getProperties().list(System.out);
}
}
有什么办法可以解决这个问题吗?我们将不胜感激所有帮助。
最佳答案
属性是一个键值对,重点是对。我不清楚你想用三个元素做什么,但它可能就像你宁愿这样做一样简单
p.setProperty( "mykey20" , "somevalue20" );
p.setProperty( "mykey30" , "somevalue30" );
...
干杯,
关于java - 创建我自己的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17254567/