zoukankan      html  css  js  c++  java
  • IO流之Properties(配置文件)

    Properties: 

    • Properties是hashtable的子类
    • Properties具备map集合的特点,且它存储的键值对都是字符串
    • Properties是集合与IO技术的综合应用的集合容器
    • 可以用于键值对形式的配置文件
    • 使用Properties加载数据时,需要数据有固定的格式:键=值

     Demo(set、get、stringPropertyNames方法演示):

     1 import java.util.*;
     2 class PropertiesTest 
     3 {
     4     public static void main(String[] args) 
     5     {
     6         method_1();
     7     }
     8 
     9     public static void method_1()
    10     {
    11         Properties prop = new Properties();
    12 
    13         //往Properties集合中塞东西进去
    14         prop.setProperty("ZhangSan","21");
    15         prop.setProperty("LiSi","25");
    16         prop.setProperty("WangWu","30");
    17 
    18         //stringPropertyNames()返回集合中的所有键,将其存储到Set中
    19         Set<String> values = prop.stringPropertyNames();
    20         for (String name : values)
    21         {
    22             //getProperty(键)方法根据键返回值
    23             System.out.println(prop.getProperty(name));
    24         }
    25     }
    26 }
    View Code

    Demo(load方法读取文件中的信息)

     1 import java.io.*;
     2 import java.util.*;
     3 class PropertiesTest2 
     4 {
     5     public static void main(String[] args)throws IOException
     6     {
     7         method();
     8     }
     9 
    10     public static void method()throws IOException
    11     {
    12         //创建Properties对象prop用于保存文件中的数据
    13         Properties prop = new Properties();
    14         //创建字符读取流对象并关联硬盘中的文件
    15         BufferedReader bufr = new BufferedReader(new FileReader("info.txt"));
    16         String line = null;
    17         //一次读一行,读完为止
    18         while ((line=bufr.readLine())!=null)
    19         {
    20             //将读到的那行数据用=切割保存到数组中
    21             String [] values = line.split("=");
    22             //将数组中的数据添加到prop集合中
    23             prop.setProperty(values[0],values[1]);
    24         }
    25         //将prop集合中的键全部读取放到keys集合中
    26         Set<String> keys = prop.stringPropertyNames();
    27         //遍历keys集合并打印
    28         for (String key : keys)
    29         {
    30             System.out.println("key:"+key+".....value:"+prop.getProperty(key));
    31         }
    32 
    33     }
    34 }
    View Code
  • 相关阅读:
    sql2005事务的使用
    [原]using的另一种用法
    [原]Cache的简单用法
    [原] Js动态删除行(支持FireFox)
    [原]为什么文本框高度不一样?
    [原]如何把object解析为int,double,float?
    压缩SQL SERVER日志文件
    [原]替换的更新(Update)查询
    [原]让链接点击过后无虚线
    [原]取得Access表中表的名字
  • 原文地址:https://www.cnblogs.com/gzc911/p/4947478.html
Copyright © 2011-2022 走看看