Properties

public class Properties
extends Hashtable<Object,Object>
Properties类表示一组持久的属性。 Properties可以保存到流中或从流中加载。 属性列表中的每个键及其对应的值都是一个字符串

 

 

方法:

public String getProperty(String key)
使用此属性列表中指定的键搜索属性。 如果在此属性列表中找不到该键,则会默认属性列表及其默认值递归。 如果找不到属性,该方法返回null

public Object setProperty(String key,String value)
Hashtable方法put 。 提供与getProperty方法的并行性 。 强制使用字符串的属性键和值。 返回的值是Hashtable调用put的结果。

Set<String> stringPropertyNames()
返回此属性列表中的一组键,其中键及其对应的值为字符串,包括默认属性列表中的不同键,如果尚未从主属性列表中找到相同名称的键。

void store(OutputStream out, String comments)
将此属性列表(键和元素对)写入此 Properties表中,以适合于使用 load(InputStream)方法加载到 Properties表中的格式输出流。
void store(Writer writer, String comments)
将此属性列表(键和元素对)写入此 Properties表中,以适合使用 load(Reader)方法的格式输出到输出字符流。

    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        properties.setProperty("湖南", "长沙");
        properties.setProperty("江苏", "南京");
        properties.setProperty("山东", "济南");
        properties.setProperty("湖北", "武汉");
        
        Set<String> keys = properties.stringPropertyNames();//获取所有的keys,相当于keySet();
        for(String key:keys) {
            System.out.println(key+"="+properties.getProperty(key));
        }
        
        //把properies的值写入文件
        
        properties.store(new FileWriter("D:\\7_Joyce\\prop.properties"), "Store Data");
        
    }

 

 

 

 

void load(InputStream inStream)
从输入字节流读取属性列表(键和元素对)。
void load(Reader reader)
以简单的线性格式从输入字符流读取属性列表(关键字和元素对)。

        Properties readProperties = new Properties();
        readProperties.load(new FileReader("D:\\7_Joyce\\prop.properties"));
        //遍历reaProperties得到值:
        Set<String> items = readProperties.stringPropertyNames();
        for(String item:items) {
            System.out.println(item+"=>"+readProperties.getProperty(item));

 

posted @ 2020-11-23 18:04  Joyce502  阅读(166)  评论(0)    收藏  举报