Java读取.properties资源文件的常用2种方法
2013-04-07 15:39
776 查看
在Java项目开发中,经常需要读取.properties资源类型资源文件。例如数据库配置文件,spring配置文件,用户自定义配置文件等待。下面简单分享自己常用的2中Java读取配置文件的方法:
1、使用ResourceBundle
public class Resources { public static final Log LOG = LogFactory.getLog(Resources.class.getName()); private static final String PREFIX = "conf."; //包名称 private ResourceBundle resource; public Resources(String fileName){ try { resource = ResourceBundle.getBundle(PREFIX + fileName, Locale.getDefault()); //eg. conf/mysql.properties } catch (MissingResourceException e) { // TODO: handle exception e.printStackTrace(); } } public String get(String key){ String ret = null; ret = resource.getString(key); if(ret == null){ LOG.error("key : "+key +" not existed or value of the key is null"); } return ret.trim(); } }
2、使用Properties
public class Resources { private Properties pro; public Resources() throws IOException{ FileInputStream in = new FileInputStream("mysql.properties"); pro = new Properties(); pro.load(in); in.close(); } public String get(String key){ return pro.getProperty(key); } }
相关文章推荐
- java读取jar包中的资源文件或properties配置文件路径的方法
- Java读取properties配置文件常用方法
- java读取jar包中的资源文件或properties配置文件路径的方法
- java读取properties资源文件的六种方法
- java中properties文件的常用读取方法
- Java读取properties配置文件时,中文乱码解决方法
- java读取properties文件的几种方法
- 用Java读取Properties文件六种方法
- Java读取Properties文件的六种方法
- Properties读取资源文件的四种方法
- java读取properties属性文件值方法
- Java读取Properties文件的六种方法
- java.util.Properties 读取资源文件
- java读取.properties配置文件的几种方法
- JAVA读取资源文件的N种方法
- java 读取properties 文件的几种方法与NoClassDefFoundError错误
- java各种读取properties文件方法
- Java读取Properties文件的六种方法
- Java读取Properties文件的六种方法
- Java读取Properties文件的六种方法