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

java 中properties 类读取k-v配置文件

时间:2017-10-14 19:47:48      阅读:362      评论:0      收藏:0      [点我收藏+]

标签:localhost   main   leo   bin   ext   int   closed   oca   utf-8   

properties 读取的配置文件key和values都是string 类型

技术分享
package com.bjsxt.others.pro;

import java.util.Properties;

/**
 * Properties 资源配置文件的读写
 * 1、key 与value 只能为字符串
 * 2、存储与读取
 * setProperty(String key, String value) 
 * getProperty(String key, String defaultValue)  
 * @author Administrator
 *
 */
public class Demo01 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        //创建对象
        Properties pro =new Properties();
        //存储
        pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver");
        //pro.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl");
        pro.setProperty("user", "scott");
        pro.setProperty("pwd", "tiger");
        
        //获取
        String url =pro.getProperty("url","test");
        System.out.println(url);
    }

}
demo1
技术分享
package com.bjsxt.others.pro;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * 使用Properties 输出到文件
 * 资源配置文件:
 * 
 * 1、.properties
 * store(OutputStream out, String comments) 
    store(Writer writer, String comments) 
   2、.xml
   storeToXML(OutputStream os, String comment)  :UTF-8字符集
   storeToXML(OutputStream os, String comment, String encoding) 
    

 * @author Administrator
 *
 */
public class Demo02 {

    /**
     * @param args
     * @throws IOException 
     * @throws FileNotFoundException 
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        //创建对象
        Properties pro =new Properties();
        //存储
        pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver");
        pro.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl");
        pro.setProperty("user", "scott");
        pro.setProperty("pwd", "tiger");
        
        //存储到e:/others  绝对路径  盘符:
        //pro.store(new FileOutputStream(new File("e:/others/db.properties")), "db配置");
        //pro.storeToXML(new FileOutputStream(new File("e:/others/db.xml")), "db配置");
        //使用相对路径 当前的工程
//        pro.store(new FileOutputStream(new File("db.properties")), "db配置");
//        pro.store(new FileOutputStream(new File("src/db.properties")), "db配置");
        pro.store(new FileOutputStream(new File("src/com/bjsxt/others/pro/db.properties")), "db配置");
    }

}
demo2
技术分享
package com.bjsxt.others.pro;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

/**
 * 使用Properties读取配置文件
 * 资源配置文件:
 * 使用相对与绝对路径读取
 * load(InputStream inStream) 
   load(Reader reader) 
   loadFromXML(InputStream in) 
 * @author Administrator
 *
 */
public class Demo03 {

    /**
     * @param args
     * @throws IOException 
     * @throws FileNotFoundException 
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        Properties pro=new Properties();
        //读取 绝对路径
        //pro.load(new FileReader("e:/others/db.properties"));
        //读取 相对路径
        pro.load(new FileReader("src/com/bjsxt/others/pro/db.properties"));
        System.out.println(pro.getProperty("user", "bjsxt"));
    }

}
demo3
技术分享
package com.bjsxt.others.pro;

import java.io.IOException;
import java.util.Properties;

/**
 * 使用类相对路径读取配置文件
 *  bin  
 * @author Administrator
 *
 */
public class Demo04 {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        Properties pro =new Properties();
        //类相对路径的 / bin 
        //pro.load(Demo04.class.getResourceAsStream("/com/bjsxt/others/pro/db.properties"));
        //"" bin 
        pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("com/bjsxt/others/pro/db.properties"));
        System.out.println(pro.getProperty("user", "bjsxt"));
    }

}
demo4

 

java 中properties 类读取k-v配置文件

标签:localhost   main   leo   bin   ext   int   closed   oca   utf-8   

原文地址:http://www.cnblogs.com/ou-pc/p/7668079.html

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