Java读取Property配置文件

jopen 5年前

经常需要读取Property配置文件,就写了个工具类,主要还是使用java.util.Properties, 简单封装了一下。

Java读取Property文件:

import java.io.File;    import java.io.FileInputStream;    import java.io.FileNotFoundException;    import java.io.IOException;    import java.util.Properties;    /**    * 读取properties文件工具类    */    public class PropertiesUtil {                /**        * 获取指定路径下property文件中的某个字段(没有默认值)        * @param key 字段        * @param filePath 属性文件        * @return 与key对应的value        * @throws FileNotFoundException if property file doesn't exists        * @throws IOException if there is some exception when load from property file        */        public static String getProperties(String key,String filePath) throws FileNotFoundException, IOException{            File file = new File(filePath);            Properties props = new Properties();            props.load(new FileInputStream(file));            return props.getProperty(key);        }                /**        * 获取指定路径下property文件中的某个字段(可以设置默认值)        * @param key 字段        * @param value 默认值        * @param filePath 属性文件        * @return 与key对应的value        * @throws FileNotFoundException if property file doesn't exists        * @throws IOException if there is some exception when load from property file        */        public static String getProperties(String key,String defaultValue,String filePath) throws FileNotFoundException, IOException{            File file = new File(filePath);            Properties props = new Properties();            props.load(new FileInputStream(file));            return props.getProperty(key,defaultValue);        }    }