配置文件读取properties(一)

该文主要记录通过properties来读取配置文件。简单介绍了两种方法。源代码下载地址
方法一:使用p.load(ClassGetResourceAsStream.class.getResourceAsStream(“conf/sd.properties”));方法加载配置文件
代码如下

package test.com.sd.properties;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

public class ClassGetResourceAsStream {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        UseProperties up=new UseProperties();
        up.useProperties1();

    }
    public void useProperties1(){
        //File的根路径是项目名File f=new File("src/conf/sd.properties");
        //而/conf/crawler.properties的跟人路径是src:class.getResourceAsStream("/conf/sd.properties");

        Properties p=new Properties();
        try {

            p.load(ClassGetResourceAsStream.class.getResourceAsStream("conf/sd.properties"));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //File f=new File(src/conf/sd.properties);
        if(p.containsKey("sd")){
            System.out.println(p.getProperty("sd"));
            /*String s=null;
            try {
                s = new String(p.getProperty("sd").getBytes("ISO-8859-1"),"UTF-8");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(s);*/
        }

    }

}

输出结果:
中文需要转码

方法二:使用
isr=new InputStreamReader(new FileInputStream(f));
p.load(isr);
加载配置文件

package test.com.sd.properties;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;

public class UseProperties {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        UseProperties up=new UseProperties();
        up.useProperties1();
    }
    private void useProperties1(){
        //File的根路径是项目名File f=new File("src/conf/sd.properties");
        //而/conf/crawler.properties的跟人路径是src:class.getResourceAsStream("/conf/sd.properties");
        //或File f=new File("conf1/sd.properties");
        File f=new File("src/conf/sd.properties");
        Properties p=new Properties();
        //FileInputStream fi;
        InputStreamReader isr;
        try {
            //无需转码
            isr=new InputStreamReader(new FileInputStream(f));
            p.load(isr);
            //也可用下面的方法但要转码
            /*fi=new FileInputStream(f);
            p.load(fi);*/

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //File f=new File(src/conf/sd.properties);
        if(p.containsKey("sd")){
            System.err.println(p.getProperty("sd"));
            //可以通过下面转码
            /*
            String s=null;
            try {
                s = new String(p.getProperty("sd").getBytes("ISO-8859-1"),"UTF-8");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(s);*/
        }
        //下面是几种遍历配置文件的方法,不刚兴趣的不用看仅自己练习才写的
        Enumeration enu=p.propertyNames();
        while(enu.hasMoreElements()){
            String key=(String) enu.nextElement();
            String value=p.getProperty(key);
            System.out.println("enumeration遍历方法:"+key+"---"+value);
            //不能像下面这么输出不然输出内容会有误:enu.nextElement()了两回然从而会使得key对应到下一个key值的value。
            //System.out.println(enu.nextElement()+":"+properties.getProperty((String)enu.nextElement()));
        }
        Iterator<Entry<Object,Object>> ite=p.entrySet().iterator();
        while(ite.hasNext()){
            String key=(String) ite.next().getKey();
            //不能想下面这样不然犯上面同样的错
            //String value=(String) ite.next().getValue();
            String value=p.getProperty(key);
            System.err.println("Iterator遍历方法:"+key+"---"+value);
        }
        for(Entry<Object,Object> entry:p.entrySet()){
            System.out.println("entry遍历方法:"+entry.getKey()+"---"+entry.getValue());
        }
    }

}

输出结果:
中文需要转码
enumeration遍历方法:ss—231lkj
enumeration遍历方法:sd—中文需要转码
enumeration遍历方法:sm—sdfadf
Iterator遍历方法:ss—231lkj
Iterator遍历方法:sd—中文需要转码
Iterator遍历方法:sm—sdfadf
entry遍历方法:ss—231lkj
entry遍历方法:sd—中文需要转码
entry遍历方法:sm—sdfadf

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值