码迷,mamicode.com
首页 > 编程语言 > 详细

java基础知识回顾之java集合类-Properties集合

时间:2014-08-01 13:13:51      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   java   使用   os   io   文件   

/**
 java.lang.Object
   |--java.util.Dictionary<K,V>
      |--java.util.Hashtable<Object,Object>
          |--java.util.Properties

 * @author Administrator
 *Properties集合:
         * 特点:
         * 1,该集合中的键和值都是字符串类型。
         * 2,集合中的数据可以保存到流中,或者从流加载
         * 3,表示一个持久的属性集,可以把内存里面的数据保存到硬盘上的文件当中。
         * 4,此类是线程安全的,可以用在多线程当中
         * 通常该集合用于操作以键值对形式存在的配置文件。
         *
         *  存数据:public Object setProperty(String key,String value) 此方法是线程同步的,调用HashTable的put方法
             取数据:public String getProperty(String key):返回属性列表里面建对应的值
             遍历:  public Set<String> stringPropertyNames():返回属性列表中的键key集
                        public Enumeration<?> propertyNames():返回属性列表中所有键的枚举


             与IO输入输出流相关的操作:
                    1.public void list(PrintStream out):用于调试
  
                   2.将属性集合中的键值信息写入字节输出流
                   public void store(OutputStream out,String comments)throws IOException
                   将属性集合中的键值信息写入字符输出流
                   public void store(Writer writer,String comments) throws IOException
                 
                   3.从输入”字节流“中读属性列表(键-值)。从文件当中读取信息到输入流中,然后从输入流中读取到集合中
                   public void load(InputStream inStream) throws IOException 从
                   从输入"字符流"中读取属性列表
                   public void load(Reader reader) throws IOException
                   
 */   

测试代码如下:


public
class PropertiesDemo { public static void main(String[]args) throws IOException{ method_5(); } /** * * Properties结合的存和取元素,遍历 */ public static void method_1(){ Properties pr = new Properties(); pr.setProperty("张三", "20"); pr.setProperty("李四", "23"); pr.setProperty("王五", "34"); pr.setProperty("赵六", "45"); /*//遍历返回一个String<Set>集合遍历,java 1.6后出来的新方法 //遍历方法1 Set<String>keys = pr.stringPropertyNames(); for(String key : keys ){ String value = pr.getProperty(key);//返回键对应的value值 System.out.println(key+"="+value); }*/ //遍历2,之前使用的老方法 Enumeration enumer = pr.propertyNames(); while(enumer.hasMoreElements()){ String key = (String) enumer.nextElement(); String value = pr.getProperty(key); System.out.println(key+"="+value); } } /** * 返回当前系统属性集合,包括java虚拟机的信息,运行时环境,操作系统信息 * */ public static void method_2(){ Properties pro = System.getProperties();//返回值 Properties pro.list(System.out); } /** * 将集合中的键-值信息存储到文件当中,使用OutputStream字节流,使用store方法 * */ public static void method_3(){ Properties pr = new Properties(); pr.setProperty("zhangshan", "20"); pr.setProperty("lisi", "23"); pr.setProperty("wangwu", "34"); pr.setProperty("zhaoliu", "45"); OutputStream os = null; try { os = new FileOutputStream("info.txt");//持久化到文件当中 pr.store(os, "info");//将集合中的键-值信息写入到输出流中 } catch (IOException e) { e.printStackTrace(); }finally{ if(os != null){ try { os.close();//保存到文件当中 } catch (IOException e) { e.printStackTrace(); } } } } /** * 将资源文件信息读入到读入到输入流中,从输入流中读取属性列表 * 1.集合中数据来自于一个文件 * 2.文件中的数据是键值对 * 3.使用load方法从输入流中读取数据 * * * 读取.properties文件 * Properties pro = new Properties(); * 打开classpath下面的指定的配置文件,读取properties配置文件 * InputStream in = getClass() * .getClassLoader() * .getResourceAsStream("log4j.properties"); * pro.load(in); * 然后对Properties集合进行遍历 */ public static void method_4(){ Properties pro = new Properties(); InputStream in = null; try { //关联已有的属性文件 in = new FileInputStream("info.txt"); pro.load(in);//load到集合中 pro.list(System.out);//使用调试方法遍历集合,打印到控制台 } catch (IOException e) { e.printStackTrace(); }finally{ try { if(in != null){ in.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 如果不存在创建文件,文件存在,读取文件并对已有问价进行修改 * @throws IOException * * info.txt文件中: * #update zhaoliu #Fri Aug 01 11:13:46 CST 2014 zhangshan=20 lisi=23 zhaoliu=30 wangwu=34 说明:看store源码知道: 注释内容都用#号开头 写入当前时间 key-value对 */ public static void method_5() throws IOException{ File file = new File("info.txt"); if(!file.exists()){ file.createNewFile(); } //通过字符输入流对文件进行读取 Reader fr = new FileReader(file); Properties pro = new Properties(); //从字符输入流中读取文件列表 pro.load(fr); //修改配置文件信息 pro.setProperty("zhaoliu", "30"); //写入到字符输出流中,持久化修改后的文件 Writer fw = new FileWriter(file); pro.store(fw, "update zhaoliu"); //遍历修改后的文件 Set<String> keys = pro.stringPropertyNames(); for(String key:keys){ String value = pro.getProperty(key); System.out.println(key+"="+value); } fr.close(); fw.close(); } }

 

java基础知识回顾之java集合类-Properties集合,布布扣,bubuko.com

java基础知识回顾之java集合类-Properties集合

标签:style   blog   color   java   使用   os   io   文件   

原文地址:http://www.cnblogs.com/200911/p/3884543.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!