目录
Properties是Hashtable的子类,也就说它具备Map集合的特点。而且它里面存储的键值对都是字符串。
Properties是集合和IO技术相结合的集合容器。该对象的特点:可以用于键值对形式的配置文件。加载数据时,需要数据有固定格式:键=值。
常用方法
String getProperty(String key):用指定的键在此属性列表中搜索属性。
Object setProperty(String key, String value):调用 Hashtable 的方法 put。
Set<String> stringPropertyNames():返回此属性列表中的键集,其中该键及其对应值是字符串,如果在主属性列表中未找到同名的键,则还包括默认属性列表中不同的键。
void load(InputStream inStream):从输入流中读取属性列表(键和元素对)。
void load(Reader reader):按简单的面向行的格式从输入字符流中读取属性列表(键和元素对)。
void store(OutputStream out, String comments):以适合使用 load(InputStream) 方法加载到 Properties 表中的格式,将此Properties表中的属性列表(键和元素对)写入输出流。
void store(Writer writer, String comments):以适合使用 load(Reader) 方法的格式,将此 Properties 表中的属性列表(键和元素对)写入输出字符。
void list(PrintStream out):将属性列表输出到指定的输出流。
void list(PrintWriter out):将属性列表输出到指定的输出流。
示例
import java.io.*;
import java.util.*;
class PropertiesDemo{
public static void main(String[] args)throws IOException{
loadDemo();
}
public static void loadDemo()throws IOException{
Properties prop=new Properties();
FileInputStream fis=new FileInputStream("info.txt");
// 将流中的数据加载进集合
prop.load(fis);
prop.setProperty("wangwu","39");
FileOutputStream fos=new FileOutputStream("info.txt");
prop.store(fos,"haha"); // 将修改的数据写入保存到文本文件,参数二为注释信息
// System.out.println(prop);
prop.list(System.out);
fis.close();
fos.close();
}
/*
演示,如何将流中的数据存储到集合中。
想要将info.txt中键值数据存到集合中进行操作。
1.用一个流和info.txt文件关联。
2.读取一行数据,将该行数据用"="进行切割。
3.等号左边作为键,右边作为值,存入到Properties集合中即可。
*/
public static void method_1()throws IOException{
BufferedReader bufr=new BufferedReader(new FileReader("info.txt"));
Properties prop = new Properties();
String line=null;
while((line=bufr.readLine())!=null){
String[] arr=line.split("=");
prop.setProperty(arr[0],arr[1]);
}
bufr.close();
System.out.println(prop);
}
// 设置和获取元素
public static void setAndGet(){
Properties prop=new Properties();
prop.setProperty("zhangsan","30");
prop.setProperty("lisi","39");
// System.out.println(prop);
String value=prop.getProperty("lisi");
// System.out.println(value);
prop.setProperty("lisi",89+"");
Set<String> names=prop.stringPropertyNames();
for(String s:names){
System.out.println(s+":"+prop.getProperty(s));
}
}
}
案例:配置文件应用
分析
需求:
用于记录应用程序运行次数,如果使用次数已到,那么给出提示。
难点分析:
很容易想到的是:计数器。
计数器定义在程序中,随着程序的运行而在内存中存在,并进行自增。可是随着该应用程序的退出,该计数器也在内存中消失了。下一次再启动该程序,又重新开始从0计数。
目的:
程序即使结束,该计数器的值也存在。下次程序自动再会加载该计数器的值并加一后在重新存储起来。
对策:
建立一个配置文件,用于记录该软件的使用次数。该配置文件使用键值对的形式。便于阅读数据,并操作数据。
实现:
键值对数据是Map集合;数据是以文件形式存储,使用IO技术;那么Map+IO-->properties.
扩展应用:
配置文件可以实现应用程序数据的共享。
编码
import java.io.*;
import java.util.*;
class RunCount{
public static void main(String[] args)throws IOException{
runCount();
}
// 记录应用程序运行次数
public static void runCount()throws IOException{
Properties prop=new Properties();
File file=new File("count.ini");
if(!file.exists())
file.createNewFile();
FileInputStream fis=new FileInputStream(file);
prop.load(fis); // 加载数据到Properties集合
int count=0;
String value=prop.getProperty("time");
if(value!=null){
count=Integer.parseInt(value);
if(count>=5){
System.out.println("您好,体验次数已达上限,请充值!");
return;
}
}
count++;
prop.setProperty("time",count+"");
FileOutputStream fos=new FileOutputStream(file);
prop.store(fos,""); // 保存修改的数据到配置文件
fos.close();
fis.close();
}
}