Properties集合
概述
map接口下的子类,因此具有键值成对存在、键唯一的特点。开发中常用于存取应用的配置项。
Properties集合存储数据
public static void main(String[] args) {
FileWriter fw = null;
try{
//创建Properties对象
Properties ppt = new Properties();
//集合中存储元素
ppt.put("name","Jimmy");
ppt.put("age","24");
//创建字符输出流
fw = new FileWriter("D:\\临时文件夹\\测试\\新建文件夹\\data.properties");
//Properties对象调用store方法,完成文件和格式内容的创建。
ppt.store(fw,"Personal Data");
}catch (IOException e){
e.printStackTrace();
}finally {
try {
if (fw != null)
fw.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
Properties集合读取数据
public static void main(String[] args) {
FileReader fr = null;
try{
//创建Properties对象
Properties ppt = new Properties();
//创建字符输入流
fr = new FileReader("D:\\临时文件夹\\测试\\新建文件夹\\data.properties");
//Properties对象调用load(Reader r)加载输入流内容
ppt.load(fr);
//Properties对象调用getProperty(String key)获得值方法
String name = ppt.getProperty("name");
String age = ppt.getProperty("age");
System.out.println("name="+name+",age="+age);
}catch (IOException e){
e.printStackTrace();
}finally {
try {
if (fr != null)
fr.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
浙公网安备 33010602011771号