读取Java中配置文件的两种方式

来源:互联网 发布:网络大电影收片 编辑:程序博客网 时间:2024/06/06 06:52
package com.fwd;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Properties;import java.util.ResourceBundle;public class ReadPro {/** * @author fwd * 2016年12月21日20:34:48 * 读取配置文件的两种方式 * 配置文件里面的格式是  * key1=value1 * key2=value2 * key3=value3 * 配置文件名字file.properties放在根目录下 * @throws IOException  *  */public static void main(String[] args) throws IOException {//read1();read2();}/** * 第二种方式 * 利用ResourceBundle.getBundle加上文件名,直接通过key获取值 */public static void read2(){String key1 = ResourceBundle.getBundle("file").getString("key1");String key2 = ResourceBundle.getBundle("file").getString("key2");String key3 = ResourceBundle.getBundle("file").getString("key3");System.out.println(key1 +"--"+key2+"--"+key3);}/** * 第一种方方式 * 利用properties对象 是java util包下的 * load(InputStream inStream)方法 * @throws IOException  */public static void read1() throws IOException{Properties p = new Properties();//创建输入流对象InputStream in = new FileInputStream("file1.properties");//把文件输入流放在对象里面p.load(in);String value1 = p.getProperty("key1");String value2 = p.getProperty("key2");String value3 = p.getProperty("key3");System.out.println(value1 +"--"+value2+"--"+value3);}}

1 0
原创粉丝点击