2017.6.29 java读取.properties配置文件的几种方法
2017-06-29 14:02
876 查看
参考来自:http://www.cnblogs.com/s3189454231s/p/5626557.html
关于路径的解释:http://blog.csdn.net/bluishglc/article/details/38753047
官方文档:http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResourceAsStream%28java.lang.String%29
关于类名.class.getClassLoader.getSystemResourceAsStream和ClassLoader.getSystemResourceAsStream中,路径的解释可以参考:http://blog.csdn.net/bluishglc/article/details/38753047
简而言之就是:
类名.class.getClassLoader.getSystemResourceAsStream(path)中填写的path:以这个类的所在路径(这里是com.lyh.test)为基础。
ClassLoader.getSystemResourceAsStream(path)中填写的path:以classpath的路径(这里是src)为基础。
最重要的是:这里全是相对路径,所以开头不要加“/”。
如果不确定当前的classpath路径,可以通过如下代码获取:
1.1.1 load(InputStream inStream)
1.1.2 load(Reader reader)
ResourceBundle是一个抽象类,这种方式来获取properties属性文件不需要加.properties后缀名,只需要文件名即可。
2.2 从InputStream中读取
获取inputStream的方法和前面load中介绍的一样。
关于路径的解释:http://blog.csdn.net/bluishglc/article/details/38753047
官方文档:http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResourceAsStream%28java.lang.String%29
0 获取InputStream的几种方式
项目结构://PropertiesWay.class.getResourceAsStream()最终调用是ClassLoader.getResourceAsStream() 1 InputStream inStream1 = PropertiesWay.class.getClassLoader().getResourceAsStream("../resource/test.properties"); InputStream inStream2 = ClassLoader.getSystemResourceAsStream("com/lyh/resource/test.properties"); InputStream inStream3 = new FileInputStream(new File("E:/lyh/file/workspace/ReadPropertiesFile/src/com/lyh/resource/test.properties")); //servlet环境下还可以用context //InputStream inStream4 = context.getResourceAsStream("/WEB-INF/config/login.conf"); //InputStream in = context.getResourceAsStream("filePath"); ////通过url获取 //URL url = new URL("path"); //InputStream inStream5 = url.openStream();
关于类名.class.getClassLoader.getSystemResourceAsStream和ClassLoader.getSystemResourceAsStream中,路径的解释可以参考:http://blog.csdn.net/bluishglc/article/details/38753047
简而言之就是:
类名.class.getClassLoader.getSystemResourceAsStream(path)中填写的path:以这个类的所在路径(这里是com.lyh.test)为基础。
ClassLoader.getSystemResourceAsStream(path)中填写的path:以classpath的路径(这里是src)为基础。
最重要的是:这里全是相对路径,所以开头不要加“/”。
如果不确定当前的classpath路径,可以通过如下代码获取:
PropertiesWay.class.getClassLoader().getResource("").toString();
1 通过jdk提供的java.util.Properties类
在操作之前,首先要读取配置文件,有两种方式:load和loadFromXML。1.1 load
load有两个方法的重载:load(InputStream inStream)、load(Reader reader),所以,可根据不同的方式来加载属性文件。1.1.1 load(InputStream inStream)
1 p.load(inStream); 2 System.out.println(p.getProperty("name")); 3 p.setProperty("name", "inStream changed");
1.1.2 load(Reader reader)
1.2 loadFromXML
2.[b]通过java.util.ResourceBundle类读取[/b]
2.1 通过ResourceBundle.getBundle()ResourceBundle是一个抽象类,这种方式来获取properties属性文件不需要加.properties后缀名,只需要文件名即可。
ResourceBundle resource = ResourceBundle.getBundle("com/mmq/test");//test为属性文件名,放在包com.mmq下,如果是放在src下,直接用test即可 2 String key = resource.getString("username");
2.2 从InputStream中读取
获取inputStream的方法和前面load中介绍的一样。
ResourceBundle resource = new PropertyResourceBundle(inStream); 2 String key = resource.getString("username");
相关文章推荐
- 2017.6.29 java读取.properties配置文件的几种方法
- java对配置文件.properties文件读取的几种方法
- java读取配置文件 xml properties的几种方法
- java读取.properties配置文件的几种方法
- [可用] java读取.properties配置文件的几种方法
- java读取.properties配置文件的几种方法
- java读取.properties配置文件的几种方法
- java读取.properties配置文件的几种方法
- java读取.properties配置文件的几种方法 .
- java读取.properties配置文件的几种方法
- java读取.properties配置文件的几种方法
- java读取.properties配置文件的几种方法
- java读取.properties配置文件的几种方法
- java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)
- java读取.properties配置文件的几种方法
- java读取.properties配置文件的几种方法
- Java中从properties配置文件中读取信息的几种方法
- java读取.properties配置文件的几种方法
- java读取.properties配置文件的几种方法
- java读取.properties配置文件的几种方法