Java:读取未知数量的属性
我有一个 Java 属性文件,定义如下:
Property.1=value1
Property.2=value2
...
这里可以有任意数量的属性。
我熟悉如何读取和使用 Java 属性,但是当我不知道属性的数量时,我不确定如何编写读取属性的代码。我对伪代码的想法是这样的:
// Somehow get the number of properties
for (int i=0; i<properties.size(); i++ {
prop.getProperty("Property"+i);
...
}
有人知道如何读取可变数量的属性吗?
I have a Java properties file defined as something like:
Property.1=value1
Property.2=value2
...
There could be any number of properties here.
I am familiar with how to read and use Java Properties, but I am not sure how I would code reading properties when I don't know the number of them. My idea of pseudo-code would be something like:
// Somehow get the number of properties
for (int i=0; i<properties.size(); i++ {
prop.getProperty("Property"+i);
...
}
Does anyone know how to read a variable number of properties?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的属性按照您的示例所示按顺序编号:
If your properties are numbered sequentially as your example suggests:
java.util.Properties
类能够从输入流加载。http:// /download.oracle.com/javase/6/docs/api/java/util/Properties.html#load%28java.io.InputStream%29
只需传递一个新的
Properties
对象即可输入流来自文件并让它滚动。The
java.util.Properties
class has the ability to load from an input stream.http://download.oracle.com/javase/6/docs/api/java/util/Properties.html#load%28java.io.InputStream%29
Simply pass a new
Properties
object the input stream from the file and let it roll.您可能想看看 Commons Collections 类 ExtendedProperties 及其方法 子集
这允许您获取具有给定前缀的所有属性(例如“Property”)
You may want to take a look at Commons Collections class ExtendedProperties and it's method subset
That allows you to get all properties with given prefix ('Property.' for example)