Properties是Hashtable的子类;
也就是说它具备map集合的特点,而且它里面存储的键值对都是字符串;
是集合中和IO技术相结合的集合容器;
该对象的特点;可以用于键值对形式的配置文件;
Properties为什么存在?
配置信息可以存放在内存中,比如说用Map集合存放,但是缺陷是一旦关闭应用,会清除对应的缓存数据,那么配置信息会回复成默认配置;
所以我们应该将配置信息持久化到硬盘,properties就是用来存放IO流从磁盘上读取的配置信息的容器;
package com.javaxl.io.Properties; import java.util.Properties; import java.util.Set; /** * @author 小李飞刀 * @site www.javaxl.com * @company * @create 2019-06-13 16:21 */ public class PropertiesDemo { public static void main(String[] args) { Properties p = new Properties(); p.setProperty("zs","30"); p.setProperty("ls","39"); System.out.println(p);//{ls=39, zs=30} String value = p.getProperty("ls"); System.out.println(value);//39 p.setProperty("ls",89+""); Set<String> names = p.stringPropertyNames(); for (String name : names) { System.out.println(name+":"+p.getProperty(name)); } } }
演示:如何将流中的数据存储到集合中;
想要将info.txt中简直数据存到集合中进行操作;
package com.javaxl.io.Properties; import java.io.*; import java.util.Properties; /** * @author 小李飞刀 * @site www.javaxl.com * @company * @create 2019-06-13 16:21 */ public class PropertiesDemo2 { public static void main(String[] args) throws IOException { // method_1(); method_2(); } /** * 将硬盘中的数据读到内存中 * 思路: * 1、用一个流和jdbc.properties文件关联; * 2、读取一行数据,将该行数据用“=”进行切割; * 3、等号左边作为键,右边作为值,存入到Properties集合中即可; */ public static void method_1() throws IOException { BufferedReader br = new BufferedReader(new FileReader("C:\\jdbc.properties")); String line = null; Properties p = new Properties(); while ((line = br.readLine())!=null){ String[] split = line.split("="); p.setProperty(split[0],split[1]); } System.out.println(p); br.close(); } /** * 给jdbc.properties配置文件中添加配置信息 * 思路: * 1、将硬盘中的jdbc.properties配置信息读到内存 * 2、给内存中的配置信息,添加新的配置 * 3、将新的配置信息持久化到硬盘 * @throws IOException */ public static void method_2() throws IOException { BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\jdbc.properties")); Properties p = new Properties(); p.load(bis); p.setProperty("admin","小李飞刀"); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\jdbc.properties")); p.store(bos,"www.javaxl.com"); p.list(System.out);//打印出properties中的所有配置信息 bis.close(); bos.close(); } }
setProperty是修改内存中的配置信息
Store是将当前的properties对象用输出流写入指定的位置,并加入注释
需求:记录程序的运行次数
用于记录应用程序的运行次数;
如果使用次数已到,那么给出注册提示;
很容易想到的是:计数器;
可是该计数器定义在程序中,随着程序的运行而在内存中存在,并进行自增,可是随着该应用程序的退出,该计数器也在内存中消失了;
下一次再启动该程序,又重新开始从0计数;
这样不是我们想要的;
程序即使结束,该计数器的值也存在;
下次程序启动时,会先加载该计数器的值并加1后再重新存储起来;
所以要建立一个配置文件,用于记录该软件的使用次数;
该配置文件使用键值对的形式;
这样便于阅读数据,并操作数据;
键值对数据就是map集合;
数据是以文件形式存储,使用IO流技术;
那么Map+IO -->Properties
配置文件可以实现应用程序数据的共享;
package com.javaxl.io.Properties;
import java.io.*;
import java.util.Properties;
/**
* @author 小李飞刀
* @site www.javaxl.com
* @company
* @create 2019-06-13 16:21
*/
public class PropertiesDemo3 {
public static void main(String[] args) throws IOException {
File file = new File("C:\\count.ini");
if(!file.exists()){
file.createNewFile();
}
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
Properties p = new Properties();
p.load(bis);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
String time = p.getProperty("times");
if(time == null){
p.setProperty("times","1");
}else {
p.setProperty("times",Integer.valueOf(time)+1+"");
}
p.store(bos,null);
if(Integer.valueOf(time) >= 5){
System.out.println("你好,使用次数已到,拿钱!!!");
return;
}
p.list(System.out);
bis.close();
bos.close();
}
}
相对于FileInputStream对象,使用File对象是不会出现异常的
备案号:湘ICP备19000029号
Copyright © 2018-2019 javaxl晓码阁 版权所有