我有一个配置值的键/值对列表,我想将其存储为Java属性文件,然后进行加载和遍历。

问题:

我是否需要将文件存储在与装入它们的类相同的包中,或者它应该放置在任何特定的位置? 文件是否需要以任何特定的扩展名结束,或者。txt OK? 如何在代码中加载文件 我如何遍历里面的值?


当前回答

你可以将一个InputStream传递给属性,所以你的文件几乎可以在任何地方,并被称为任何东西。

Properties properties = new Properties();
try {
  properties.load(new FileInputStream("path/filename"));
} catch (IOException e) {
  ...
}

迭代:

for(String key : properties.stringPropertyNames()) {
  String value = properties.getProperty(key);
  System.out.println(key + " => " + value);
}

其他回答

有很多方法来创建和读取属性文件:

将文件存储在同一个包中。 推荐.properties扩展名,但你可以选择自己的。 使用java中的这些类。util package =>属性,ListResourceBundle, ResourceBundle类。 要读取属性,请使用properties或java.lang.System类的迭代器或枚举器或直接方法。

ResourceBundle类:

 ResourceBundle rb = ResourceBundle.getBundle("prop"); // prop.properties
 System.out.println(rb.getString("key"));

属性类:

Properties ps = new Properties();
ps.Load(new java.io.FileInputStream("my.properties"));

我在去年写了关于这个属性框架的文章。 它将提供多种方式来加载属性,并具有强类型。

看看http://sourceforge.net/projects/jhpropertiestyp/

jhpropertiestype将为开发人员提供强类型的属性。 易于在现有项目中集成。 由属性类型的大型系列处理。 提供通过属性IO实现单行初始化属性的能力。 使开发人员能够创建自己的属性类型和属性io。 网页演示也可用,屏幕截图如上所示。 也有一个标准的web前端实现来管理属性,如果你选择使用它。

项目网页上有完整的文档、教程、javadoc、faq等。

属性已经成为遗产。首选项类优先于属性。

偏好数据的层次集合中的一个节点。这个类允许应用程序存储和检索用户和系统首选项以及配置数据。此数据持久地存储在依赖于实现的后备存储区中。典型的实现包括平面文件、特定于操作系统的注册表、目录服务器和SQL数据库。该类的用户不需要关心后台存储的细节。

与基于字符串的键值对的属性不同,Preferences类有几个方法用于在Preferences数据存储中获取和放置基本数据。我们只能使用以下类型的数据:

字符串 布尔 双 浮动 int 长 字节数组

要加载属性文件,可以提供绝对路径,如果属性文件在类路径中,则使用getResourceAsStream()。

package com.mypack.test;

import java.io.*;
import java.util.*;
import java.util.prefs.Preferences;

public class PreferencesExample {

    public static void main(String args[]) throws FileNotFoundException {
        Preferences ps = Preferences.userNodeForPackage(PreferencesExample.class);
        // Load file object
        File fileObj = new File("d:\\data.xml");
        try {
            FileInputStream fis = new FileInputStream(fileObj);
            ps.importPreferences(fis);
            System.out.println("Prefereces:"+ps);
            System.out.println("Get property1:"+ps.getInt("property1",10));

        } catch (Exception err) {
            err.printStackTrace();
        }
    }
}

xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE preferences SYSTEM 'http://java.sun.com/dtd/preferences.dtd'>
<preferences EXTERNAL_XML_VERSION="1.0">
<root type="user">
<map />
<node name="com">
  <map />
  <node name="mypack">
    <map />
    <node name="test">
      <map>
        <entry key="property1" value="80" />
        <entry key="property2" value="Red" />
      </map>
    </node>
  </node>
</node>
</root>
</preferences>

看看这篇关于偏好商店内部的文章

加载属性文件:

Properties prop = new Properties();
InputStream stream = ...; //the stream to the file
try {
  prop.load(stream);
} finally {
  stream.close();
}

我把.properties文件放在一个存放所有配置文件的目录中,我没有把它与访问它的类放在一起,但这里没有限制。

对于这个名字…为了冗长起见,我使用。properties,如果你不想的话,我认为你不应该把它命名为。properties。

下面是遍历属性的另一种方法:

Enumeration eProps = properties.propertyNames();
while (eProps.hasMoreElements()) { 
    String key = (String) eProps.nextElement(); 
    String value = properties.getProperty(key); 
    System.out.println(key + " => " + value); 
}