网上文章常见的几种读取.properties文件的方式
来源:互联网 发布:淘宝达人就是微淘吗 编辑:程序博客网 时间:2024/06/10 16:58
1、使用java.util.Properties类的load()方法 示例:
- InputStream in = lnew BufferedInputStream(new FileInputStream(name));
- Properties p = new Properties();
- p.load(in);
2、使用java.util.ResourceBundle类的getBundle()方法
示例:
- ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
用ResourceBundle读取.properties文件可避免路径问题
我在jar里读取.properties文件时,总是找不到文件路径,后来用ResourceBundle读取.properties文件即可避免路径问题,代码如下:
//process为文件名,切记不要加 .properties, URL是文件里的键名
- ResourceBundle bundle = ResourceBundle.getBundle("com.ihandy.smsoc.app.process");
- String s = bundle.getString("URL");
- System.out.println(s);
- pURL = s;
3、使用java.util.PropertyResourceBundle类的构造函数
示例:
- InputStream in = new BufferedInputStream(new FileInputStream(name));
- ResourceBundle rb = new PropertyResourceBundle(in);
4、使用class变量的getResourceAsStream()方法
示例:
- InputStream in = 类名.class.getResourceAsStream(name);
- Properties p = new Properties();
- p.load(in);
5、使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法示例:
- InputStream in = 类名.class.getClassLoader().getResourceAsStream(name);
- Properties p = new Properties();
- p.load(in);
6、使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法示例:
- InputStream in = ClassLoader.getSystemResourceAsStream(name);
- Properties p = new Properties();
- p.load(in);
7、Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法示例:
- InputStream in = context.getResourceAsStream(path);
- Properties p = new Properties();
- p.load(in);
=========================================================================
转载地址:http://han2000lei.iteye.com/blog/377899
0 0
- 网上文章常见的几种读取.properties文件的方式
- 网上文章常见的几种读取.properties文件的方式
- 网上文章常见的几种读取.properties文件的方式
- properties文件读取的几种方式
- Properties 文件 读取 的几种方式
- 读取properties文件的几种方式
- java几种读取.properties文件的方式
- 读取properties文件的几种常用方式
- java几种读取.properties文件的方式
- 读取Properties配置文件的几种方式
- 读取Properties文件的几种方法
- 读取Properties文件的几种方法
- 读取properties文件的几种方法
- 加载.properties文件的几种方式
- 读取.properties文件的两种方式
- properties文件的读取方式
- 读取properties文件的方式
- java读取.properties配置文件 的几种方式
- iOS 第三方开源库
- [leetcode-209]Minimum Size Subarray Sum(java)
- 海量数据处理系列之:用C++实现Bitmap算法
- Kindeditor与Jquery冲突 textarea得不到数据
- hdu(3555)——Bomb(不要49)
- 网上文章常见的几种读取.properties文件的方式
- 用*号输出菱形图案
- Android基础入门教程——5.1 Fragment基本概述
- 简单之局部函数保护,加密等预防措施(动态内存)
- cocos2d-x & cocostudio bug记录
- Restore versus recovery
- PHP的内存释放问题
- 张孝祥java视频学习笔记(四)
- appium源码分析(六)-find(下)