设为首页收藏本站

Scripts 学盟

 找回密码
 加入学盟

QQ登录

只需一步,快速开始

查看: 1623|回复: 4
打印 上一主题 下一主题

java对properties文件的操作 [复制链接]

Rank: 8Rank: 8

风雨相伴

跳转到指定楼层
1#
Snail 实名认证  发表于 2011-9-28 23:12:57 |只看该作者 |倒序浏览
本帖最后由 Snail 于 2011-10-28 23:26 编辑

1. 资源文件所存放的位置
   资源文件妨碍classpath下,即工程项目的class包下

2. 获取系统资源文件的方式有2中
  1.    a.  通过  InputStream inputstream = ClassLoader.getSystemResourceAsStream("info.properties");
  2.    b. 通过 InputStream inputstream = this.getClass().getResourceAsStream("/info.properties");
复制代码
采用第一种方式获取资源文件时,文件不以"/" 开头,而采用方法b的话,文件必须"/"开头

3. 提取加载资源文件的信息
Java代码
  1. Properties properties = new Properties();
  2. InputStream inputstream = ClassLoader.getSystemResourceAsStream("info.properties");
  3. // InputStream inputstream = this.getClass().getResourceAsStream("/info.properties");

  4. properties.load(inputstream);
复制代码
4. 操作资源文件
  1.    a. 根据key值在资源文件中查询value值
  2.       1. getProperty(String key) 用指定的键在此属性列表中搜索属性。
  3.       2. getProperty(String key, String defaultValue)   用指定的键在属性列表中搜索属性。
  4.      
  5.    b. 获取所有的键值对的信息
复制代码
Java代码
  1. Enumeration<String> enumvalue = (Enumeration<String>) properties.propertyNames();// 返回属性列表中所有键的枚举,如果在主属性列表中未找到同名的键,则包括默认属性列表中不同的键

  2. while (enumvalue.hasMoreElements())
  3. {
  4.       String key = enumvalue.nextElement();
  5.       System.out.println(key + " : " + properties.getProperty(key));
  6. }


  7.    c. 向资源文件中添加键值信息,如果key值相同就会将原有的信息覆盖
  8.         
  9. Java代码
  10. URL url = ClassLoader.getSystemResource("info.properties");
  11. File file = new File(url.toURI());
  12.         
  13. InputStream is = new FileInputStream(file);
  14. properties.load(is);
  15. properties.setProperty("key", "value");

  16. OutputStream fos = new FileOutputStream(file);
  17. properties.store(fos, null);

  18. fos.flush();
  19. is.close();



  20.   d. 删除相关的键值对
  21.       
  22. Java代码

  23. File file = new File(ClassLoader.getSystemResource("info.properties").toURI());
  24. InputStream is = new FileInputStream(file);

  25. properties.load(is);
  26. properties.remove("key");

  27. OutputStream fos = new FileOutputStream(file);
  28. properties.store(fos, null);

  29. is.close();
  30. fos.flush();
  31. fos.close();File file = new File(ClassLoader.getSystemResource("info.properties").toURI());
  32. InputStream is = new FileInputStream(file);

  33. properties.load(is);
  34. properties.remove("key");

  35. OutputStream fos = new FileOutputStream(file);
  36. properties.store(fos, null);

  37. is.close();
  38. fos.flush();
  39. fos.close();
复制代码
分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
分享分享0 收藏收藏0
命运的手,推我向前!何处是停歇!

版主

狼贝雷

Rank: 7Rank: 7Rank: 7

2#
菜鸟贝雷 发表于 2011-10-28 10:26:28 |只看该作者
不错,灰常有用!
忘战必危,忘学必废。

使用道具 举报

管理员

超级大菜鸟

Rank: 9Rank: 9Rank: 9

3#
混混@普宁.中国 实名认证  发表于 2011-10-28 11:11:42 |只看该作者
之前一直用 xml 做配置文件, 现在反例觉 properties 文件挺方便实用的
又不需要第三方组件来解释

使用道具 举报

Rank: 8Rank: 8

风雨相伴

4#
Snail 实名认证  发表于 2011-10-28 18:15:38 |只看该作者
菜鸟贝雷 发表于 2011-10-28 10:26
不错,灰常有用!

命运的手,推我向前!何处是停歇!

使用道具 举报

Rank: 8Rank: 8

风雨相伴

5#
Snail 实名认证  发表于 2011-10-28 18:16:24 |只看该作者
混混@普宁.中国 发表于 2011-10-28 11:11
之前一直用 xml 做配置文件, 现在反例觉 properties 文件挺方便实用的
又不需要第三方组件来解释 ...

我也有同感
命运的手,推我向前!何处是停歇!

使用道具 举报

您需要登录后才可以回帖 登录 | 加入学盟

手机版|Scripts 学盟   |

GMT+8, 2024-5-9 17:41 , Processed in 1.059212 second(s), 12 queries .

Powered by Discuz! X2

© 2001-2011 Comsenz Inc.

回顶部