黑马程序员:Properties类介绍:集合中和IO技术相结合的集合容器

来源:互联网 发布:handwrite软件 编辑:程序博客网 时间:2024/06/06 12:39

---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------

Properties是hashtable的子类

它具备Map集合的特点,而且它里面存储的键值对都是字符串,
是集合中和IO技术相结合的集合容器

该对象的特点:可用于键值对形式的配置文件
那么在加载数据时,通常有固定的格式 键=值

设置和获取元素
Properties prop = new Properties();
prop.setProperty("zhangsan","30"); //调用Hashtable的put方法 
prop.setProperty("zhangsan","98");//修改zhangsan的value值


Set<String> names = prop.stringPropertyNames();
for(String s : names)
{
System.out.println(s+"~"+prop.getProperty(s)); //循环便利Set集合中的键对值
}


如何将流中的数据存储到集合中:
将info.txt中键值数据存到集合中进行操作
思想:
1.用一个流和info.txt文件关联
2.读去一行数据,将该行数据"="进行切割
3.等号左边作为键,右边作为值,存入到Properties集合中即可

使用两种方式实现上述需求:1.Properties对象的load方法 2.普通方法
其中需要修改其中一个参数的值,并保存进info.txt
代码如下:
import java.io.BufferedReader;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.OutputStreamWriter;import java.util.Properties;/* *  如何将流中的数据存储到集合中:将info.txt中键值数据存到集合中进行操作思想:1.用一个流和info.ini文件关联2.读去一行数据,将该行数据"="进行切割3.等号左边作为键,右边作为值,存入到Properties集合中即可使用两种方式实现上述需求:1.Properties对象的load方法 2.load的原理 */public class PropertiesLoadMethod {public static void main(String[] args) throws IOException{// TODO Auto-generated method stub//Properties pr = getInfo("info.ini");//pr.list(System.out);//System.out.println(pr.getProperty("XBus.dll"));Properties pr = loadInfo("info.ini");pr.list(System.out);OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("info2.ini"));pr.store(osw, "info2");FileWriter fos = new FileWriter("info3.ini");pr.store(fos, "info3");}//load方法public static Properties loadInfo(String filename){BufferedReader bfr = null;try{bfr = new BufferedReader(new FileReader(filename));Properties pr = new Properties();pr.load(bfr);return pr;}catch (IOException io){throw new RuntimeException("文件读取失败");}finally{try{if(bfr != null)bfr.close();}catch (IOException io){throw new RuntimeException("文件关闭失败");}}}//load的原理public static Properties getInfo(String filename){BufferedReader bfr = null;try{bfr = new BufferedReader(new FileReader(filename));Properties pr = new Properties();String len = null;while((len=bfr.readLine())!=null){String[] str = len.split("=");if (str.length==2)pr.setProperty(str[0],str[1]);elsepr.setProperty(str[0],"");}return pr;}catch (IOException i){throw new RuntimeException("文件读取失败");}finally{try{if (bfr!=null)bfr.close();}catch(IOException i){throw new RuntimeException("文件关闭失败");}}}}



Properties练习:
用于记录应用程序运行次数(次数的读取和存储)
如果使用次数已到(比如达到5次),那么给出注册提示

代码如下:
import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.Properties;/*Properties练习:用于记录应用程序运行次数(次数的读取和存储)如果使用次数已到(比如达到5次),那么给出注册提示思路:1.需要一个计数器,程序运行一次计数器就自增一次2.判断是否存在配置文件,若不存在则创建3.判断文档中计数器的值,若不存在,则创建,若存在,则获取计数器值,计数器自增后写入文件4.当获取的计数器为5的时候,提示需要注册 */public class PropertiesRecordRunTimes {public static void main(String[] args) {// TODO Auto-generated method stubtry{getCount();}catch(IOException io){throw new RuntimeException("配置文件操作异常");}}public static void getCount() throws IOException{File ini = new File("count.ini");Properties pro = new Properties();if(!ini.exists()){pro.setProperty("time","1");pro.store(new FileOutputStream("count.ini"), "哈哈");}else{BufferedInputStream bis = new BufferedInputStream(new FileInputStream("count.ini"));pro.load(bis);int count = Integer.parseInt(pro.getProperty("time"));if (count>=5){System.out.println("运行超过5次,想继续使用请续费");return;}count++;pro.setProperty("time",count+"");pro.store(new FileOutputStream("count.ini"), "哈哈");bis.close();}}}

---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net
原创粉丝点击