java加载properties文件的六种基本方式

来源:互联网 发布:高斯滤波器的算法 编辑:程序博客网 时间:2024/05/29 10:23

        今天在学习spring的时候,突然想到用工具类的方式来加载properties文件,这样就可以在不改变代码的情况下,更改配置信息,以下是java加载properties文件的六种基本方式的代码:

package com.test.modul.utils;import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.util.Locale;import java.util.Properties;import java.util.PropertyResourceBundle;import java.util.ResourceBundle;/** * 加载properties文件的方式 * * @author wjzuo * */public class LoadPropertiesFileUtil {    private static String basePath = "src/main/java/com/test/modul/utils/prop.properties";    private static String path = "";    /**     * 一、 使用java.util.Properties类的load(InputStream in)方法加载properties文件     *     * @return     */    public static String getPath1() {        try {            InputStream in = new BufferedInputStream(new FileInputStream(                    new File(basePath)));            Properties prop = new Properties();            prop.load(in);            path = prop.getProperty("path");        } catch (FileNotFoundException e) {            System.out.println("properties文件路径书写有误,请检查!");            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        return path;    }    /**     * 二、 使用java.util.ResourceBundle类的getBundle()方法     * 注意:这个getBundle()方法的参数只能写成包路径+properties文件名,否则将抛异常     *     * @return     */    public static String getPath2() {        ResourceBundle rb = ResourceBundle                .getBundle("com/test/modul/utils/prop");        path = rb.getString("path");        return path;    }    /**     * 三、 使用java.util.PropertyResourceBundle类的构造函数     *     * @return     */    public static String getPath3() {        InputStream in;        try {            in = new BufferedInputStream(new FileInputStream(basePath));            ResourceBundle rb = new PropertyResourceBundle(in);            path = rb.getString("path");        } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        return path;    }    /**     * 四、 使用class变量的getResourceAsStream()方法     * 注意:getResourceAsStream()方法的参数按格式写到包路径+properties文件名+.后缀     *     * @return     */    public static String getPath4() {        InputStream in = LoadPropertiesFileUtil.class                .getResourceAsStream("/com/test/modul/utils/prop.properties");        Properties p = new Properties();        try {            p.load(in);            path = p.getProperty("path");        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        return path;    }    /**     * 五、     * 使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法     * getResourceAsStream(name)方法的参数必须是包路径+文件名+.后缀     * 否则会报空指针异常     * @return     */    public static String getPath5() {        InputStream in = LoadPropertiesFileUtil.class.getClassLoader()                .getResourceAsStream("com/test/modul/utils/prop.properties");        Properties p = new Properties();        try {            p.load(in);            path = p.getProperty("path");        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        return path;    }    /**     * 六、 使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法     * getSystemResourceAsStream()方法的参数格式也是有固定要求的     *     * @return     */    public static String getPath6() {        InputStream in = ClassLoader                .getSystemResourceAsStream("com/test/modul/utils/prop.properties");        Properties p = new Properties();        try {            p.load(in);            path = p.getProperty("path");        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        return path;    }    public static void main(String[] args) {        System.out.println(LoadPropertiesFileUtil.getPath1());        System.out.println(LoadPropertiesFileUtil.getPath2());        System.out.println(LoadPropertiesFileUtil.getPath3());        System.out.println(LoadPropertiesFileUtil.getPath4());        System.out.println(LoadPropertiesFileUtil.getPath5());        System.out.println(LoadPropertiesFileUtil.getPath6());    }}

其中第一、四、五、六种方式都是先获得文件的输入流,然后通过Properties类的load(InputStream inStream)方法加载到Properties对象中,最后通过Properties对象来操作文件内容;

第二、三中方式是通过ResourceBundle类来加载Properties文件,然后ResourceBundle对象来操做properties文件内容。

其中最重要的就是每种方式加载文件时,文件的路径需要按照方法的定义的格式来加载,否则会抛出各种异常,比如空指针异常。

0 0