Properties类
简介:
Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置。
主要的方法:
1. getProperty ( String key),用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。
2. load ( InputStream inStream),从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。
3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。
4. store ( OutputStream out, String comments),以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
5. clear (),清除所有装载的 键 - 值对。该方法在基类中提供。
读取文件的方法:
InputStream in = getClass().getResourceAsStream("资源Name");
或者
InputStream in = new BufferedInputStream(new FileInputStream(filepath));
例子:
配置文件
文件名:Test.properties
name=wangwei
phone=18334702840
读取
public class getProperties {
public static void main(String[] args) throws FileNotFoundException, IOException {
Properties pps = new Properties();
//pps.load(new FileInputStream("Test.properties"));
getProperties gP = new getProperties();
String FileName = "Test.properties";
InputStream is=gP.getClass().getResourceAsStream(FileName);
pps.load(is);
Enumeration enum1 = pps.propertyNames();//得到配置文件的名字
while(enum1.hasMoreElements()) {
String strKey = (String) enum1.nextElement();
String strValue = pps.getProperty(strKey);
System.out.println(strKey + "=" + strValue);
}
}
}
最新文章
- 温故而知新 css + html 超级牛逼的居中策略
- 18.有一个网页地址, 比如PHP开发资源网主页: http://www.phpres.com/index.html,如何得到它的内容?
- c++ 顺序容器适配器
- Atitit 发帖机系列(7) 词法分析的方法attilax大总结)
- kettle转换提高性能拆分转换步骤_20161201
- 适用于Magento的最合适的.htaccess写法
- R.java不能自动更新
- Python字节流打包拆包
- 一起简单写一下AIDL,入个门
- cocos2d制作动态光晕效果基础——blendFunc
- android WebView将新浪天气为我所用 ------>;仅供娱乐
- 权限控制框架Spring Security 和Shiro 的总结
- MySql 中文乱码解决办法
- C语言程序设计(基础)- 第14、15周作业
- xml解析方式之JAXP解析入门
- 苹果ios系统无法通过RD Client连接win10服务器远程错误0x00001307
- 十大web安全扫描工具
- django + nginx + uwsgi
- SHA1加密工具
- Bogus URL svn: is not properly URI-encoded
热门文章
- mysql ----BaseDao工具类
- PostgreSQL 空间数据类型point、 line等
- setTimeout setInterval 计时器
- Kafka集群安装部署、Kafka生产者、Kafka消费者
- 关于python 的http 日常操作
- locust启动命令
- document.documentElement.clientHeight和document.body.clientHeight区别
- vue2 inheritAttrs、attrs和attrs和listeners使用
- Log4Net 常见错误提示(不断更新中)
- WSGI 的简单理解