Properties
类表示了一个持久的属性集。Properties
可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
一个属性列表可包含另一个属性列表作为它的“默认值”;如果未能在原有的属性列表中搜索到属性键,则搜索第二个属性列表。
因为 Properties
继承于 Hashtable
,所以可对 Properties
对象应用 put
和 putAll
方法。但不建议使用这两个方法,因为它们允许调用者插入其键或值不是 String
的项。相反,应该使用 setProperty
方法。如果在“不安全”的 Properties
对象(即包含非 String
的键或值)上调用 store
或 save
方法,则该调用将失败。类似地,如果在“不安全”的 Properties
对象(即包含非 String
的键)上调用 propertyNames
或 list
方法,则该调用将失败。
Properties的setProperties和getProperty方法:
常用方法及示例:
String | getProperty(String key) 用指定的键在此属性列表中搜索属性。 |
String | getProperty(String key, String defaultValue) 用指定的键在属性列表中搜索属性。 |
void | list(PrintStream out) 将属性列表输出到指定的输出流。 |
void | list(PrintWriter out) 将属性列表输出到指定的输出流。 |
void | load(InputStream inStream) 从输入流中读取属性列表(键和元素对)。 |
void | load(Reader reader) 按简单的面向行的格式从输入字符流中读取属性列表(键和元素对)。 |
Object | setProperty(String key, String value) 调用 Hashtable 的方法 put 。 |
void | store(OutputStream out, String comments) 以适合使用 load(InputStream) 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。 |
void | store(Writer writer, String comments) 以适合使用 load(Reader) 方法的格式,将此 Properties 表中的属性列表(键和元素对)写入输出字符。 |
/**
* @Description: Properties操作文件时,通常要求文件有固定的格式:key = value
*/
public static void main(String[] args) {
Properties prop=new Properties();
//内存中设置
// memoryProp(prop);
//磁盘中加载配置文件
// loadProp(prop);
//磁盘中加载的原理
myLoadProp(prop);
//将配置信息保存进磁盘文件中
saveProp(prop,"d:\\prop.txt");
}
//内存中设置
public static void memoryProp(Properties prop){
//设置值
prop.setProperty("zhangsan","man");
prop.setProperty("lili","woman");
System.out.println("prop"+prop);
//获取值
String sex=prop.getProperty("zhangsan");
System.out.println("zhangsan's sex:"+sex);
//类似hashMap里的keyset 获取所有键
Set<String> set=prop.stringPropertyNames();
for (String str: set ) {
System.out.println(str+":"+prop.getProperty(str));
}
}
properties对象 .load(输入流);
//磁盘中加载配置文件
public static void loadProp(Properties prop){
try {
//字节流方式加载
// FileInputStream fis=new FileInputStream("d:\\propTest.txt");
// prop.load(fis);
//字符流方式加载
FileReader fr=new FileReader("d:\\propTest.txt");
prop.load(fr);
prop.list(System.out);
fr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//磁盘中加载的自定义实现 大致原理
public static void myLoadProp(Properties prop){
try {
//读取文本用字符输入流
FileReader fr=new FileReader("d:\\propTest.txt");
//使用字符流缓冲
BufferedReader br=new BufferedReader(fr);
String line=null;
//每次读取一行
while ((line=br.readLine())!=null){
//拿到一行的配置,设置进prop中
String[] entry=line.split("=");
prop.setProperty(entry[0],entry[1]);
}
prop.list(System.out);
fr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
配置信息持久化:properties对象.store(输出流);
public static void saveProp(Properties prop,String filePath){
FileOutputStream fos=null;
FileWriter fw=null;
try {
//字节流保存
// fos=new FileOutputStream(filePath);
// prop.store(fos,"xxx's properties");
//字符流保存
fw=new FileWriter(filePath);
prop.store(fw,"xxx's properties");
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(fos!=null)fos.close();
if(fw!=null) fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
将它写入文件