---------------------------java属性集合Properties类详解--------------------------------
Properties是Hashtable集合的子类,也是线程安全的。
Properties
类表示了一个持久的属性集。Properties
可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
特点:
1. 继承于Hashtable,是线程安全的键值对存储结构。
2. Properties可保存在流中或从流中加载。
3. 只能保存字符串的键值对。
构造器
Properties() |
Properties(Properties defaults) |
常用方法
getProperty(String key) | |
getProperty(String key, String defaultValue) | |
例子:创建Properties集合添加键值对,并且打印
public class PropTest {
public static void main(String[] args) {
//属性集合类不支持泛型
Properties prop = new Properties();
//添加键值对
prop.setProperty("name", "zhangsan");
prop.setProperty("age", "10");
prop.setProperty("gender", "male");
//打印prop
System.out.println(prop);
//输出{age=10, name=zhangsan, gender=male}
//获取键值对
String name = prop.getProperty("name");
String age = prop.getProperty("age");
String gender = prop.getProperty("gender");
System.out.println(name + age + gender);
//输出zhangsan10male
}
}
范例:将Properties集合中的键值对写入到文件中
方法一:使用遍历方式写入
public class PropTest2 {
public static void main(String[] args) {
//属性集合类不支持泛型
Properties prop = new Properties();
//添加键值对
prop.setProperty("name", "zhangsan");
prop.setProperty("age", "10");
prop.setProperty("gender", "male");
PrintWriter pw = null;
try {
//创建自动刷新字符打印流对象
pw = new PrintWriter(new FileWriter("prop.txt"),true);
//拿到prop中所有的key
Set keys = prop.keySet();
//遍历所有的key
for (Object obj : keys) {
//将obj强转成String
String key = (String)obj;
//通过key获取到相对应的值
String value = prop.getProperty(key);
//key和值写入到文件中
pw.println(key + "=" + value);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(pw != null)
pw.close();
}
}
}
void | list(PrintStream out) |
void | list(PrintWriter out) |
方法二:使用Properties中的list()方法写入文件
public class PropTest3 {
public static void main(String[] args) {
//属性集合类不支持泛型
Properties prop = new Properties();
//添加键值对
prop.setProperty("name", "zhangsan");
prop.setProperty("age", "10");
prop.setProperty("gender", "male");
PrintWriter pw = null;
try {
//创建自动刷新字符打印流对象
pw = new PrintWriter(new FileWriter("prop1.txt"),true);
//使用list()方法,把prop中的键值对写入到文件中
prop.list(pw);
} catch (IOException e) {
e.printStackTrace();
}finally {
if(pw != null)
pw.close();
}
}
}
========================================================================
void | load(InputStream inStream) |
void | load(Reader reader) |
范例:把文件中的数据传入到Properties集合中,使用Properties中的load()方法
注意: 实际项目中使用Properties的文件来存储数据,不使用txt
fr= new FileReader("src/prop.properties");
public class PropTest4 {
public static void main(String[] args) {
//属性集合类不支持泛型
Properties prop = new Properties();
FileReader fr = null;
try {
fr = new FileReader("prop.txt");
//把文件中的字符读取到prop集合中
prop.load(fr);
//获取prop中的属性
String name = prop.getProperty("name");
String age = prop.getProperty("age");
String gender = prop.getProperty("gender");
//打印
System.out.println(name + age + gender);
//打印zhangsan10male
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
=====================================================================
void | store(OutputStream out, String comments) |
void | store(Writer writer, String comments) |
范例:把properties集合按属性格式存储到输出流文件中,使用properties中的store()方法
public class PropTest6 {
public static void main(String[] args) {
//属性集合类不支持泛型
Properties prop = new Properties();
//添加键值对
prop.setProperty("name", "zhangsan");
prop.setProperty("age", "10");
prop.setProperty("gender", "male");
FileWriter fw = null;
try {
fw = new FileWriter("prop2.txt");
//把prop存储到输出流中,第二个参数是注释
prop.store(fw, "---");
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(fw != null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}