java基础---->java中Properties的使用

  Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支 持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置。今天,我们就开始Properties的使用。

Java中Properties的使用

Properties的文档说明:

The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.

Properties类的描述:

public class Properties extends Hashtable<Object,Object>

测试的项目结构如下:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQEAAACfCAIAAAB2lfstAAAWOklEQVR4nO2d+1sT17rH/TvOj+c5x7NP7T7d1m6gtHVbtVJ7rKj76bG23a3V3XRj6W63V1CBIncUiYao3MQCwRgQqkhAqSIioBC8taA1IHJRIDcuIgyonfNDQrJmZq3JAkMmYd7Ps54+ZLLemTV2fWe9K1nfrHnsjBgwWfclqe7WxD1tT59oTxtrTTLVRWWmRO7Ym7pjb+q+JNWAydprHlm2s2Bm5591jMrgecFKo9TNAHyAeTML25ekGnl8kR2+gikvxl88GxscHCZrwKgMnudEoX+Z9s+UaWpAr5jHR5p2A55nhhrYFZPB2qrxhWU/TSpjWVZUA1P9z6vPY6My+CW7rl4xkxO8/HWBWYSogZqamuTkZNK72/bmsqYz+PL7i2U7C84bOhJPXv3ywFlcNKIBltUrvCYC0ACAAa+BixcvLliwYP78+aSwb7b/yD4qspcXPZrqssIf4rXfbSv4bltB291HZxvvL9mWvyJCc/3eo9IzNwXRWA3YO4peMW/elCbQBMTZhZBq/JTEdRAdZabOGezKv4KVRk63FAai6Rrae4UacBuL3AVMQHwSjAbsAvj4449FNPA3xQm2K4ftyhm/nxu1T6v6qfVM1wt7Odf9wsr8bq/WNjD+9dbTgmhsLmTvOZx+yRGK4y1uNddxdDhxdnDeOZGO7/oTF0h8cPM0QBkL44BPw9eAUwAbNmwQ0cCaz35kOw6N3T38feSp/aUdursTwpJdZ/lqu77n0bAgGp0TCzsuy7LCDInT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rn8NRLmn2RsRqAjYS9iDQayM3PGxiaJJXc/DxCnA9qAPtzd4gG3O/HStAAbCTsLaTRQGZefp9tAjsO9NkmMvPyCXEkDSB7VItqwH6U+0uNrr/cbwYszIWEV+Y3w+2+yKRcCDYS9hLSaECdq+m1TpCKOldDiMP3bOdMEdmgF1MT6b9TvZT3u6XuNwNmxebEWFszxb7Ijiq8OTFsJOwtpNGAMkvbbWYOaRpTcmtj1NU7D1R8G1fabWbsRZml9XqLXvLJCg9mP0YaDRw4pntoYkjlwDGd11sEGpAv0mggSX1avHi9RaAB+QJrpwG5AxoA5A5oAJA7oAFA7oAGALnzUhqw2IYyC8pUx3Wq47qcwrIno0/Rd/tsTz5NKnu55gHArPP/wWpC48VzwG0AAAAASUVORK5CYII=" alt="" />

一、在huhx.properties文件中,我们为也方便,加入一条数据:

name=huhx

二、将huhx.properties文件加载读取,得到相应的属性

Properties properties = new Properties();
FileInputStream fis = new FileInputStream("huhx.properties");
properties.load(fis);
System.out.println(properties.get("name"));

properties.load()执行之后,fis.close方法对于properties.get(key)没有什么影响。因为load方法已经将数据解析读取到了Property中。

三、Properties的list方法的使用

PrintStream printStream = System.out;
properties.list(printStream);

list方法的具体代码:

public void list(PrintStream out) {
out.println("-- listing properties --");
Hashtable h = new Hashtable();
enumerate(h);
for (Enumeration e = h.keys() ; e.hasMoreElements() ;) {
String key = (String)e.nextElement();
String val = (String)h.get(key);
if (val.length() > 40) {
val = val.substring(0, 37) + "...";
}
out.println(key + "=" + val);
}
}

四、Properties的store方法的使用

OutputStream outputStream = new FileOutputStream("huhx.txt");
properties.store(outputStream, "comments");

五、Properties的storeToXML方法的使用

OutputStream outputStream2 = new FileOutputStream("huhx.xml");
properties.storeToXML(outputStream2, "comments");

六、最终生成的文件如下:

huhx.txt:

#comments
#Thu May 19 19:19:36 CST 2016
name=huhx

huhx.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>comments</comment>
<entry key="name">huhx</entry>
</properties>

PropertiesTest.java:

package com.huhx.linux;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Properties; public class PropertiesTest { public static void main(String[] args) throws Exception {
// 一般Properties的使用
Properties properties = new Properties();
FileInputStream fis = new FileInputStream("huhx.properties");
properties.load(fis);
System.out.println(properties.get("name")); // 以下是测试的部分
PrintStream printStream = System.out;
properties.list(printStream);
OutputStream outputStream = new FileOutputStream("huhx.txt");
properties.store(outputStream, "comments");
OutputStream outputStream2 = new FileOutputStream("huhx.xml");
properties.storeToXML(outputStream2, "comments");
}
}

PropertiesTest.java

七、属性文件中占位符的使用

huhx的文件内容如下:

word=I loves {0} and {1}

java的代码如下:

String word = properties.getProperty("word");
String lovesWord = MessageFormat.format(word, "android", "linux");
System.out.println(lovesWord); // I loves android and linux

友情链接

上一篇:Zoj3332-Strange Country II(有向竞赛图)


下一篇:Unity3d 音效模块相关