Properties:
Properties是hashtable的子类(在java.util包中)。该集合的特点:可以用于键值对形式的配置文件,且不允许Key重复,若有重复的,后者会覆盖前者。
也就是说它具备map集合的特点,而且它里面存储的键值对都是字符串。
是集合中和IO技术相结合的集合容器。
Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置。像Python支持的配置文件是.ini文件,同样,它也有自己读取配置文件的类ConfigParse,方便程序员或用户通过该类的方法来修改.ini配置文件。在Java中,其配置文件常为.properties文件,格式为文本文件,文件的内容的格式是“键=值”的格式,文本注释信息可以用"#"来注释。
1,继承关系
2.常用方法
【案例演示】:通过setProperty()方法来设置属性信息。
1 import java.util.Properties; 2 import java.util.Set; 3 4 public class PropertiesDemo { 5 public static void main(String[] args) { 6 Properties properties=new Properties(); 7 properties.setProperty("zhangsan","20");//设置键值对,注意参数都是String类型 8 properties.setProperty("lisi","21"); 9 properties.setProperty("lisi","22"); 10 System.out.println(properties); 11 String value=properties.getProperty("lisi");//通过Key获取Value 12 System.out.println("李四的值为:"+value); 13 Set<String> names=properties.stringPropertyNames();//返回的是Key的集合 14 for (String name:names) { 15 System.out.println(name+"---"+properties.getProperty(name)); 16 } 17 } 18 }
运行结果:
上面那个例子是通过方法来将属性信息添加到集合中的,那如何将文件中已存在的属性信息又存储到集合中来呢?
【案例演示】:如何将流中的数据存储到集合中。
1 import java.io.*; 2 import java.util.Properties; 3 import java.util.Scanner; 4 5 public class FileToProperties { 6 public static void main(String[] args) throws IOException { 7 Scanner in=new Scanner(System.in); 8 String filename=in.nextLine(); 9 BufferedReader br=new BufferedReader(new FileReader(filename));//因为是文本,所以Reader. 10 Properties properties=new Properties();//定义一个集合 11 String str=null; 12 while ((str=br.readLine())!=null){//读取文本中的数据 13 String[] keys= str.split("="); 14 properties.setProperty(keys[0],keys[1]);//存储到集合中 15 } 16 br.close(); 17 System.out.println(properties); 18 } 19 }
运行结果:
由于上述代码操作太麻烦,所以在1.6版本之后出现了load()方法。可以直接将流对象作为参数传递给load()方法。
load(Reader reader);
load(InputStream in);
1 import java.io.*; 2 import java.util.Properties; 3 import java.util.Scanner; 4 5 public class FileToProperties { 6 public static void main(String[] args) throws IOException { 7 Scanner in=new Scanner(System.in); 8 String filename=in.nextLine(); 9 BufferedReader br=new BufferedReader(new FileReader(filename));//因为是文本,所以Reader. 10 Properties properties=new Properties();//定义一个集合 11 properties.load(br);//直接将流对象加载进集合 12 br.close(); 13 properties.list(System.out);//将属性列表输出到指定的输出流中。此处是控制台 14 } 15 }
假如你通过上述代码将配置文件中属性信息都打印出来后,发现Key为“lisi”的Value值存储错了,于是你想到了用Properties中的setProperty()方法重新设置lisi的值。
但是通过查文档你就会发现,setProperty()方法只是单纯的将内存中的值改了,而本地文件中值却没有改,当关闭电脑后,内存中的信息就会清零,那么当下次在加载配置文件时,去到lisi的Value值仍为改过之前的。
所以还要用store()方法,将集合中的信息再次写入流中,并保存到本地文档。
1 import java.io.*; 2 import java.util.Properties; 3 import java.util.Scanner; 4 5 public class FileToProperties { 6 public static void main(String[] args) throws IOException { 7 Scanner in=new Scanner(System.in); 8 String filename=in.nextLine(); 9 BufferedReader br=new BufferedReader(new FileReader(filename));//因为是文本,所以Reader. 10 Properties properties=new Properties();//定义一个集合 11 properties.load(br);//直接将流对象加载进集合 12 properties.setProperty("lisi","00");//重新设置属性 13 FileWriter fw=new FileWriter(filename); 14 properties.store(fw,"bianji");//将修改完的集合再次保存到文件中,第二个参数为注释信息 15 br.close(); 16 properties.list(System.out); 17 } 18 }
运行之前的文档信息:
运行完文档信息发生变化:(注意#号为注释信息!!)
【案例演示】:用于记录应用程序运行次数,如果使用次数已到,那么就会给出注册提示。
注意:计数前要先将配置文件加载进来,计数后要将集合数据写入文档!
还要注意计数文件是在第一次访问程序之后才创建的。
1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 import java.util.Properties; 6 7 public class UseCount { 8 public static void main(String[] args) throws IOException { 9 Properties properties = new Properties();//创建一个集合 10 File file=new File("c:\\html\\test\\demo.txt");//将要读取的文件封装成对象 11 if(!file.exists()){//先判断计数文件是否已被创建 12 file.createNewFile();//不存在则要先创建 13 } 14 FileInputStream fis = new FileInputStream(file);//将封装成对象的文件加载到输入流中 15 properties.load(fis);//然后将流加载到集合中 16 String num = properties.getProperty("count");//获取集合中Key为count的Value值 17 if(num==null){//如果value为null 18 properties.setProperty("count",Integer.toString(1)); 19 }else { 20 int count=Integer.valueOf(properties.getProperty("count")); 21 ++count; 22 if(count>5){ 23 System.out.println("次数用完!");return; 24 } 25 properties.setProperty("count",Integer.toString(count)); 26 } 27 FileOutputStream fos=new FileOutputStream(file); 28 properties.store(fos,"kk"); 29 fis.close(); 30 fos.close(); 31 } 32 }
原文:https://www.cnblogs.com/ljl150/p/12348557.html