------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
Properties类继承自Hashtable,是以键值对的形式存在的,通常用于保存配置文件,保存程序的各种属性
常用方法:getProperty(String key)通过给定的键,找到相对应的值
list(PrintWriter)将属性列表输出到指定的输出流
load(Reader reader)从输入流中读取属性列表
setProperty(String key,String value)增加一对键值对
set<String> stringPropertyName() 返回properties中存放的所有key的集合
1.如何将文件中的数据增加到以键值对的形式存储到集合中
a)建立一个和文件相对应的流对象
b)读取一行数据,并用“=”分割
c)存储到键值对集合中
在不使用Properties时:
package test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;
public class Test11 {
public static void main(String[] args) {
(new Test11()).method();
}
public void method(){
method_1();
method_2();
}
//建立一个存放键值对信息的文件,并存放部分信息
public void method_1(){
BufferedWriter bufw = null;
try {
FileWriter fw = new FileWriter("c:\\test.txt");
bufw = new BufferedWriter(fw);
Map<String,String> m = new Hashtable<String, String>();
m.put("张三", "30");
m.put("李四", "40");
m.put("王五", "50");
m.put("赵六", "60");
Set<String> strs = m.keySet();
for(String s:strs){
String str = m.get(s);
bufw.write(s+"="+str);
bufw.newLine();
bufw.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(bufw!=null)bufw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//读取文件中的信息,并存放到map中
public void method_2(){
BufferedReader bufr = null;
try {
FileReader fr= new FileReader("c:\\test.txt");
bufr = new BufferedReader(fr);
Map<String, String> m = new Hashtable<String, String>();
String s = null;
while((s=bufr.readLine())!=null){
String[] strs = s.split("=");
m.put(strs[0], strs[1]);
}
System.out.println(m);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(bufr!=null)bufr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
使用Properties时:
package test;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class Test12 {
public static void main(String[] args) {
(new Test12()).method();
}
public void method() {
method_1();
}
public void method_1() {
//Properties加载指定文件夹中的属性对
FileReader fr = null;
Properties p = null;
try {
fr = new FileReader("c:\\test.txt");
p = new Properties();
//将流中的数据加载到Properties
p.load(fr);
System.out.println(p);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (fr != null)
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//将Properties中的属性对存储到文件中
FileWriter fw = null;
try {
fw = new FileWriter("c:\\test.txt");
p.setProperty("钱七", "70");
p.setProperty("周八", "80");
p.store(fw, "");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(fw!=null)fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}