Properties
- Properties 是 HashTable 的一个子类,用于表示一个持久的属性集;
- Properties 使用键值结构存储数据,键和值都是字符串,使用与遍历方式和 HashMap 相同;
- Properties 类被许多其他的 Java 类所使用,例如,当获得系统环境值时,System.getProperties() 方法就是返回一个 Properties 对象;
- java.util.Properties,详见:Class Properties;
1. 定义
- Properties 定义了下面的实例变量,该变量包含了一个与 Properties 对象相关联的默认属性列表;
Properties default;
2. 构造方法
构造方法 | 说明 |
---|---|
Properties() | 创建一个没有默认值的 Properties 对象,属性列表为空 |
Properties(Properties propDefault) | 将 propDefault 作为其默认值的对象,属性列表为空 |
3. 类的方法
- Properties 除了继承了 HashTable 的方法外,还有自己定义的方法;
- Properties 有一个不推荐使用的方法 save(),它被 store() 取代,因为它不能正确地处理错误;
类型 | 方法 | 说明 |
---|---|---|
String | getProperty(String key) | 返回与 key 相关联的值,如果 key 既不在列表中也不在默认属性列表中,则返回一个 null 对象 |
String | getProperty(String key, String defaultProperty) | 返回用此属性列表中指定的键搜索属性值,如果 key 既不在列表中也不在默认属性列表中,则返回 defaultProperty |
void | list(PrintStream streamOut) | 将 byte 型属性列表发给与 streamOut 相连接的输出流 |
void | list(PrintWriter streamOut) | 将 char 型属性列表发给与 streamOut 相连接的输出流 |
void | load(InputStream inStream) throws IOException) | 用与 streamIn 相连接的数据流输入一个属性列表 |
Enumeration | propertyNames() | 返回关键字的枚举,也包括那些在默认属性列表中找到的关键字 |
Object | setProperty(String key, String value) | 保存一对属性,并将 value 与 key 关联,返回与 key 关联的前一个值,如果不存在这样的关联,则返回 null |
void | store(OutputStream streamOut, String description) | 在写入由 description 指定的字符串后,属性列表被写入与 streamOut 相连接的输出流 |
Set< String> | stringPropertyNames() | 所有键的名称的集合 |
4. 类的使用
- Properties 的一个有用的功能就是可以指定一个默认属性,如果没有值与特定的键相关联,则返回这个默认属性。例如,getProperty(“name”, “default value”) 中,如果 name 值没有找到,则返回 default value;
- 当构造一个 Properties 对象时,可以传递 Properties 的另一个实例作为新实例的默认值,在这种情况下,如果对一个给定的 Properties 对象调用 getProperty(“foo”),而 foo 不存在时,Java 在默认 Properties 对象中寻找 foo, 它允许默认属性的任意层嵌套;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
public class test {
public static void main(String[] args) {
Properties capitals = new Properties();
Set states;
String s;
capitals.put("China", "Beijing");
capitals.put("Japan", "Tokyo");
states = capitals.keySet();
Iterator it = states.iterator();
while (it.hasNext()) {
s = (String) it.next();
System.out.println("Country: " + s + " ,Capital: " + capitals.getProperty(s) + ".");
}
s = capitals.getProperty("France", "no data");
System.out.println("France: " + s + ".");
}
}
- 为了展现更大的灵活性,当构造一个 Properties 对象时,可指定一个默认的属性列表,如果在主列表中没有发现期望的关键字,则会搜索默认列表;
- Properties 的一个最有用之处是可以利用 store() 和 load() 方法方便地对包含在 Properties 对象中的信息进行存储或从盘中装入信息,在任何情况下,都可以将一个 Properties 对象写入流或从中将其独处,这使得属性列表特别便于实现简单的数据库;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
public class test {
public static void main(String[] args) {
Properties settings = new Properties();
try {
//每次运行都会读取count.java,直接取出文件中记录的运行次数并+1后,再将新的运行次数存回文件
settings.load(new FileInputStream("C:\\count.java"));
} catch (Exception e) {
//将属性的值设为0,表示程序还没没有被运行过时执行下行
settings.setProperty("count", Integer.toString(0));
}
int c = Integer.parseInt(settings.getProperty("count")) + 1;
System.out.println("number of use: " + c);
settings.put("count", Integer.toString(c));
try {
//用store()方法进行存储,每个属性的键和值都必须是字符串类型
//所以这里没有使用从HashTable继承的put、get,而是直接使用的Properties的setProperty(),getProperty()进行属性的设置与读取
settings.store(new FileOutputStream("C:\\count.java"), "PropertiesFile use it.");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
5. 应用案例
a. 配置内容并读出
import java.io.FileNotFoundException;
import java.util.Properties;
import java.util.Set;
public class Test {
public static void main(String[] args) throws FileNotFoundException {
//创建属性集对象
Properties properties = new Properties();
//添加键值对元素
properties.setProperty("filename", "C:\\Users\\80626\\Desktop\\1.txt");
properties.setProperty("length", "209385038");
properties.setProperty("location", "C:\\Users\\80626\\Desktop\\2.txt");
//打印属性集对象
System.out.println(properties);
//通过键,获取属性值
System.out.println(properties.getProperty("filename"));
System.out.println(properties.getProperty("length"));
System.out.println(properties.getProperty("location"));
//遍历属性集,获取所有键的集合
Set<String> strings = properties.stringPropertyNames();
//打印键值对
for (String key : strings) {
System.out.println(key + " -- " + properties.getProperty(key));
}
}
}
/*
{filename=C:\Users\80626\Desktop\1.txt, length=209385038, location=C:\Users\80626\Desktop\2.txt}
C:\Users\80626\Desktop\1.txt
209385038
C:\Users\80626\Desktop\2.txt
filename -- C:\Users\80626\Desktop\1.txt
length -- 209385038
location -- C:\Users\80626\Desktop\2.txt
*/
b. 读取文件并遍历
public void load(InputStream inStream)
:从字节输入流中读取键值对。参数中使用了字节输入流,通过流对象,可以关联到某文件上,这样就能够加载文本中的数据了;- 注意,文本中的数据,必须是键值对形式,可以使用空格、等号、冒号等符号分隔;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
/*
文件格式:
filename=a.txt
length=209385038
location=D:\a.txt
*/
public class Test {
public static void main(String[] args) throws IOException {
// 创建属性集对象
Properties pro = new Properties();
// 加载文本中信息到属性集
pro.load(new FileInputStream("C:\\Users\\80626\\Desktop\\1.txt"));
// 遍历集合并打印
Set<String> strings = pro.stringPropertyNames();
for (String key : strings) {
System.out.println(key + " -- " + pro.getProperty(key));
}
}
}
/*
输出
filename -- a.txt
length -- 209385038
location -- D:a.txt
*/
c. 读取文件更改后打印
- 源文件
Student.txt
:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
public class Answer_4 {
public static void main(String[] args) throws IOException {
Properties pro = new Properties();
pro.load(new FileReader("Student.txt"));
Set<String> strings = pro.stringPropertyNames();
for (String key : strings) {
if (key.equals("刘方")) {
pro.setProperty("刘方", "18");
}
}
//1
for (String key : strings) {
pro.store(new FileWriter("newstu.txt"), key + "=" + pro.getProperty(key));
}
//2
pro.store(new FileWriter("newstu.txt"), "create by Regino");
}
}
- 输出文件 1:
- 输出文件 2:
输出的文件中前几行是生成的注释,虽然是取消不了的,但是因为是注释,所以不会影响读取。
6. 总结
- Properties 能使用 Map 所有的API,如 get、put、entrySet、keySet。store(输出流):保存配置文件;load(输入流):读取配置文件。
- Properties 在项目中经常用于做配置,是本地配置文件,一般写入提供转换语言的文本,避免了在做频繁数据交流时过多的链接数据库的操作。
推荐阅读:Java ResourceBundle 工具类 >>>