简介:

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);
}
}
}

最新文章

  1. 温故而知新 css + html 超级牛逼的居中策略
  2. 18.有一个网页地址, 比如PHP开发资源网主页: http://www.phpres.com/index.html,如何得到它的内容?
  3. c++ 顺序容器适配器
  4. Atitit 发帖机系列(7) 词法分析的方法attilax大总结)
  5. kettle转换提高性能拆分转换步骤_20161201
  6. 适用于Magento的最合适的.htaccess写法
  7. R.java不能自动更新
  8. Python字节流打包拆包
  9. 一起简单写一下AIDL,入个门
  10. cocos2d制作动态光晕效果基础——blendFunc
  11. android WebView将新浪天气为我所用 ------>仅供娱乐
  12. 权限控制框架Spring Security 和Shiro 的总结
  13. MySql 中文乱码解决办法
  14. C语言程序设计(基础)- 第14、15周作业
  15. xml解析方式之JAXP解析入门
  16. 苹果ios系统无法通过RD Client连接win10服务器远程错误0x00001307
  17. 十大web安全扫描工具
  18. django + nginx + uwsgi
  19. SHA1加密工具
  20. Bogus URL svn: is not properly URI-encoded

热门文章

  1. mysql ----BaseDao工具类
  2. PostgreSQL 空间数据类型point、 line等
  3. setTimeout setInterval 计时器
  4. Kafka集群安装部署、Kafka生产者、Kafka消费者
  5. 关于python 的http 日常操作
  6. locust启动命令
  7. document.documentElement.clientHeight和document.body.clientHeight区别
  8. vue2 inheritAttrs、attrs和attrs和listeners使用
  9. Log4Net 常见错误提示(不断更新中)
  10. WSGI 的简单理解