java 读取properties 的不同方式,ResourceBundle和Porperties

来源:互联网 发布:方伯谦 知乎 编辑:程序博客网 时间:2024/06/04 20:01

读取properties 的方式

通过Properties类来获取:

public Properties properties;

try {

            File file=new File(pathname)

            FileInputStream fis = new FileInputStream(file);
            properties = new Properties();

            properties.load(new BufferedInputStream(fis));

            String svalue=properties.getProperty("key");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();

        }

通过ResourceBundle 类来读取:

        ResourceBundle rbd = ResourceBundle.getBundle("myproperties",Locale.getDefault());
        String str = rbd.getString("key");
        try {
            System.out.println(str);
            System.out.println(new String(str.getBytes("ISO-8859-1"),"GB2312"));//如果遇到中文乱码问题可这样转换
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

当使用ResourceBundle 读取properties 时 properties的文件必须在classpath 下,不然会报错,这个需要注意


0 0
原创粉丝点击