常见的几种读取.properties文件的方式
2016-05-19 17:32
309 查看
常见的几种读取.properties文件的方式
1、使用java.util.Properties类的load()方法 示例:
Java代码![](http://han2000lei.iteye.com/images/icon_copy.gif)
![](http://han2000lei.iteye.com/images/icon_star.png)
![](http://han2000lei.iteye.com/images/spinner.gif)
- InputStream in = lnew BufferedInputStream(new FileInputStream(name));
- Properties p = new Properties();
- p.load(in);
InputStream in = lnew BufferedInputStream(new FileInputStream(name)); Properties p = new Properties(); p.load(in);
2、使用java.util.ResourceBundle类的getBundle()方法
示例:
![](http://han2000lei.iteye.com/images/icon_copy.gif)
![](http://han2000lei.iteye.com/images/icon_star.png)
![](http://han2000lei.iteye.com/images/spinner.gif)
- ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
用ResourceBundle读取.properties文件可避免路径问题
我在jar里读取.properties文件时,总是找不到文件路径,后来用ResourceBundle读取.properties文件即可避免路径问题,代码如下:
//process为文件名,切记不要加 .properties, URL是文件里的键名
![](http://han2000lei.iteye.com/images/icon_copy.gif)
![](http://han2000lei.iteye.com/images/icon_star.png)
![](http://han2000lei.iteye.com/images/spinner.gif)
- ResourceBundle bundle = ResourceBundle.getBundle("com.ihandy.smsoc.app.process");
- String s = bundle.getString("URL");
- System.out.println(s);
- pURL = s;
ResourceBundle bundle = ResourceBundle.getBundle("com.ihandy.smsoc.app.process"); String s = bundle.getString("URL"); System.out.println(s); pURL = s;
3、使用java.util.PropertyResourceBundle类的构造函数
示例:
![](http://han2000lei.iteye.com/images/icon_copy.gif)
![](http://han2000lei.iteye.com/images/icon_star.png)
![](http://han2000lei.iteye.com/images/spinner.gif)
- InputStream in = new BufferedInputStream(new FileInputStream(name));
- ResourceBundle rb = new PropertyResourceBundle(in);
InputStream in = new BufferedInputStream(new FileInputStream(name)); ResourceBundle rb = new PropertyResourceBundle(in);
4、使用class变量的getResourceAsStream()方法
示例:
![](http://han2000lei.iteye.com/images/icon_copy.gif)
![](http://han2000lei.iteye.com/images/icon_star.png)
![](http://han2000lei.iteye.com/images/spinner.gif)
- InputStream in = 类名.class.getResourceAsStream(name);
- Properties p = new Properties();
- p.load(in);
InputStream in = 类名.class.getResourceAsStream(name); Properties p = new Properties(); p.load(in);
5、使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法 示例:
![](http://han2000lei.iteye.com/images/icon_copy.gif)
![](http://han2000lei.iteye.com/images/icon_star.png)
![](http://han2000lei.iteye.com/images/spinner.gif)
- InputStream in = 类名.class.getClassLoader().getResourceAsStream(name);
- Properties p = new Properties();
- p.load(in);
InputStream in = 类名.class.getClassLoader().getResourceAsStream(name); Properties p = new Properties(); p.load(in);
6、使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法 示例:
![](http://han2000lei.iteye.com/images/icon_copy.gif)
![](http://han2000lei.iteye.com/images/icon_star.png)
![](http://han2000lei.iteye.com/images/spinner.gif)
- InputStream in = ClassLoader.getSystemResourceAsStream(name);
- Properties p = new Properties();
- p.load(in);
InputStream in = ClassLoader.getSystemResourceAsStream(name); Properties p = new Properties(); p.load(in);
7、Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法 示例:
![](http://han2000lei.iteye.com/images/icon_copy.gif)
![](http://han2000lei.iteye.com/images/icon_star.png)
![](http://han2000lei.iteye.com/images/spinner.gif)
- InputStream in = context.getResourceAsStream(path);
- Properties p = new Properties();
- p.load(in);
相关文章推荐
- 网上文章常见的几种读取.properties文件的方式
- 网上文章常见的几种读取.properties文件的方式
- properties文件读取的几种方式
- Spring中读取properties文件的几种方式
- java几种读取.properties文件的方式
- spring读取properties和其他配置文件的几种方式
- 读取properties文件的几种方式
- 1、Web工程下读取配置文件的几种常见方式
- Properties 文件 读取 的几种方式
- java几种读取.properties文件的方式
- java读取.properties配置文件 的几种方式
- java读取配置文件 xml properties的几种方法
- 读取配置文件的几种方式
- SpringBoot读取properties文件的方式
- java对配置文件.properties文件读取的几种方法
- Java读取文件内容的几种方式
- 五种方式让你在java中读取properties文件内容不再是难题
- Java读取文件的几种方式
- java中读取properties文件内容的五种方式
- 五种方式让你在java中读取properties文件内容不再是难题