java.util.Properties读取中文内容的配置文件,发生中文乱码的现象有解决方案

本来没啥好说的,但是网上流传的关于那个绕弯方法的文章太多了,太误导人了,还是写一下以正视听吧。

直接上源码

 -------------------------------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.igrslab.spider.util;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Properties;
public class MyConfig {
    private static Properties props = new Properties();
    private static MyConfig instance=null;
    private static Reader reader = null;
     
    public static synchronized MyConfig getInstance(){
        if(instance==null){
            instance=new MyConfig();
        }
        return instance;
    }
     
    private MyConfig(){
        init();
    }
    private void init(){
        try {
            reader = new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"), "UTF-8");//编码
            props.load(reader); 
        catch (FileNotFoundException e1) {
            System.err.println("config.properties is not in the classpath. Please check it.");
            return;
            // e1.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
            return;
        }finally{
            if (null != reader){
                try {
                    reader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
     
    public String getValue(String param){
        return props.get(param)==null?null:String.valueOf(props.get(param)).trim();
    }
     
    public int getIntValue(String param){
        int value=0;
        Object obj=props.get(param);
        if(obj!=null){
            try{
                String temp=obj.toString();
                value=Integer.valueOf(temp.trim());
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
        return value;
    }
    public long getLongValue(String param){
        long value=0;
        Object obj=props.get(param);
        if(obj!=null){
            try{
                String temp=obj.toString();
                value=Long.valueOf(temp.trim());
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
        return value;
    }
    public boolean getBooleanValue(String param){
        boolean value=false;
        Object obj=props.get(param);
        if(obj!=null){
            try{
                String temp=obj.toString();
                value=Boolean.valueOf(temp.trim());
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
        return value;
    }
}

  使用这种方法要注意一下,在linux下开发的.properties,如果要用到windows上,可能发生配置项丢失的现象。

posted @   小丿blog  阅读(630)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示