Java Properties 的使用

Properties

properities是一个集合他在java中位置如图这里看图片描述
在api文档中对他的描述就是(Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。)简单的说它的特点如下:

  1. 该集合中的键和值都是字符串类型的
  2. 该集合中的数据都可以保存到流中,或者从流中获取

常用的方法属性列表:

主要类容

方法概要
StringgetProperty(String key) 用指定的键在此属性列表中搜索属性
StringgetProperty(String key, String defaultValue) 用指定的键在属性列表中搜索属性
voidlist(PrintStream out) 将属性列表输出到指定的输出流。
voidload(InputStream inStream) 从输入流中读取属性列表(键和元素对)。
ObjectsetProperty(String key, String value) 调用 Hashtable 的方法 put。
package cn.itcast.io.p2.properties;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;

public class PropertiesDemo {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {

        /*
         * Map
         *  |--Hashtable
         *      |--Properties:
         * 
         * Properties集合:
         * 特点:
         * 1,该集合中的键和值都是字符串类型。
         * 2,集合中的数据可以保存到流中,或者从流获取。
         * 
         * 通常该集合用于操作以键值对形式存在的配置文件。 
         * 
         * 
         */

//      methodDemo_4();
//      myLoad();

        test();
    }


    //对已有的配置文件中的信息进行修改。 
    /*
     * 读取这个文件。
     * 并将这个文件中的键值数据存储到集合中。
     * 在通过集合对数据进行修改。
     * 在通过流将修改后的数据存储到文件中。 
     */
    public static void test() throws IOException{
        //读取这个文件。
        File file = new File("info.txt");
        if(!file.exists()){
            file.createNewFile();
        }
        FileReader fr = new FileReader(file);




        //创建集合存储配置信息。
        Properties prop = new Properties();

        //将流中信息存储到集合中。
        prop.load(fr);

        prop.setProperty("wangwu", "16");



        FileWriter fw = new FileWriter(file);

        prop.store(fw,"");

//      prop.list(System.out);

        fw.close();
        fr.close();



    }



    //模拟一下load方法。
    public static void myLoad() throws IOException{

        Properties prop  = new Properties();

        BufferedReader bufr = new BufferedReader(new FileReader("info.txt"));

        String line = null;

        while((line=bufr.readLine())!=null){

            if(line.startsWith("#"))
                continue;

            String[] arr = line.split("=");

//          System.out.println(arr[0]+"::"+arr[1]);
            prop.setProperty(arr[0], arr[1]);
        }

        prop.list(System.out);

        bufr.close();

    }

    public static void methodDemo_4() throws IOException {  

        Properties prop  = new Properties();

        //集合中的数据来自于一个文件。 
        //注意;必须要保证该文件中的数据是键值对。
        //需要使用到读取流。 
        FileInputStream fis = new FileInputStream("info.txt");

        //使用load方法。 
        prop.load(fis);

        prop.list(System.out);



    }

    public static void methodDemo_3() throws IOException {
        Properties prop  = new Properties();

        //存储元素。 
        prop.setProperty("zhangsan","30");
        prop.setProperty("lisi","31");
        prop.setProperty("wangwu","36");
        prop.setProperty("zhaoliu","20");

        //想要将这些集合中的字符串键值信息持久化存储到文件中。
        //需要关联输出流。
        FileOutputStream fos = new FileOutputStream("info.txt");

        //将集合中数据存储到文件中,使用store方法。
        prop.store(fos, "info");

        fos.close();

    }

    /**
     * 演示Properties集合和流对象相结合的功能。
     */

    public static void methodDemo_2(){
        Properties prop  = new Properties();

        //存储元素。 
//      prop.setProperty("zhangsan","30");
//      prop.setProperty("lisi","31");
//      prop.setProperty("wangwu","36");
//      prop.setProperty("zhaoliu","20");

        prop = System.getProperties();
        prop.list(System.out);
    }

    /*
     * Properties集合的存和取。
     */

    public static void propertiesDemo(){
        //创建一个Properties集合。

        Properties prop  = new Properties();

        //存储元素。 
        prop.setProperty("zhangsan","30");
        prop.setProperty("lisi","31");
        prop.setProperty("wangwu","36");
        prop.setProperty("zhaoliu","20");

        //修改元素。 
        prop.setProperty("wangwu","26");

        //取出所有元素。
        Set<String> names = prop.stringPropertyNames();

        for(String name : names){
            String value = prop.getProperty(name);
            System.out.println(name+":"+value);
        }
    }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值