该类是以键值对的形式保存的,
Properties
可保存在流中或从流中加载。
1.getProperty(String key)方法和setProperty(String key,String value)方法
getProperty(String key)方法根据键获得value值
setProperty(String key,String value)方法将key/value保存到Properties集合中
例:
public static void main(String[] args) throws Exception
{
Properties pro = new Properties();
pro.setProperty("zhangsan","20");
pro.setProperty("lisi","30");
System.out.println(pro.getProperty("zhangsan"));
System.out.println(pro.getProperty("lisi"));
}
2.Set<String> stringPropertyNames()
返回所有的key,保存到set集合中
例:
public static void main(String[] args) throws Exception
{
Properties pro = new Properties();
pro.setProperty("zhangsan","20");
pro.setProperty("lisi","30");
pro.setProperty("wangwu","25");
Set<String> keys = pro.stringPropertyNames();
for(String key:keys){
System.out.println(key+"="+pro.getProperty(key));
}
}
3.load(InputStream in)和load(Reader reader)
从输入流中加载key/value数据到Properties中,输入流从带有key/value的文件中读取后,再通过拆分把key和value通过调用setProperty(key,value)方法保存到Properties中
(1).load(InputStream in)方法加载的原理
例:
public static void main(String[] args) throws Exception
{
File file = new File("person.txt");
InputStream input = new FileInputStream(file);
Properties pro = new Properties();
load(pro,input);//这里pro被改变
//获取pro中所有的key
Set<String> keys = pro.stringPropertyNames();
for(String key:keys){
System.out.println(key+"="+pro.getProperty(key));
}
}
public static void load(Properties pro,InputStream input)throws IOException {
//读取带有key/value的文件
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line = null;
while((line = reader.readLine())!=null){
//拆分key/value
String[] property = line.split("=");
//将key/value保存到pro中
pro.setProperty(property[0],property[1]);
}
}
(2)理解了load()方法的原理后,就可以很明确的使用load()方法了
例:
public static void main(String[] args) throws Exception {
File file = new File("person.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
Properties pro = new Properties();
//从输入流中加载属性文件信息
pro.load(reader);
//获取所有的key
Set<String> keys = pro.stringPropertyNames();
for(String key:keys){
System.out.println(key+"="+pro.getProperty(key));
}
}
4.void store(OutputStream out,String comments)和void store(Writer w,String comments)
将Properties中setProperty(key,value)设置的key/value保存到输出流中,在这个方法中因为传入了输出流,所以在这个方法中直接调用了输出流中的write()方法,
其实这个方法和下面的list()方法是一样的作用
例:
public static void main(String[] args) throws Exception
{
Properties pro = new Properties();
pro.setProperty("zhangsan","10");
pro.setProperty("lisi","20");
pro.setProperty("wangwu","30");
PrintWriter pw = new PrintWriter(new FileOutputStream("demo.txt"));
//这里不使用store()方法就不能加入到输出流中,就不能写入到文件中
pro.setProperty("amazon","45"); //使用store()方法将property中的key/value保存到输出流中
pro.store(pw,"");
//这里一定要注意关闭
pw.close();
}
5.list(OutputStream out)和list(Writer w)
通过输出流,将key/value输出,在list方法中传入了输出流,所以在list方法中会调用输出方法,所以直接使用这个方法就代表了不仅是将key/value列举出来,还会将key/value输出到目的地
例:
public static void main(String[] args) throws Exception
{
Properties pro = new Properties();
pro.setProperty("zhangsan","10");
pro.setProperty("lisi","20");
pro.setProperty("wangwu","30");
PrintWriter pw = new PrintWriter(new FileOutputStream("demo.txt"));
//在list方法中会调用println()方法输出
pro.list(pw);
//这里一定要注意关闭
pw.close();
}