Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3964581
  • 博文数量: 366
  • 博客积分: 9916
  • 博客等级: 中将
  • 技术积分: 7195
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-29 23:27
个人简介

简单!

文章分类

全部博文(366)

文章存档

2013年(51)

2012年(269)

2011年(46)

分类: Java

2012-11-09 10:09:04

       Java Properties 类读取配置文件信息在平时写程序的时候,有些参数是经常改变的,而这种改变不是我们预知的。java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"的格式,在properties文件中可以 用"#"来作注释,properties文件在Java编程中用到的地方很多,操作很方便。

1、获取JVM的系统属性
  1. import java.util.Properties;
  2.  
  3. class PropTest {
  4.     public static void main(String[] args) {
  5.     Properties pps = System.getProperties();
  6.     pps.list(System.out);
  7.     }
  8. }

2、读取配置文件
配置文件config.ini
  1. author=ZJ
  2. user=all
  3. copyright=2006-2007
获取配置文件代码
  1. import java.io.FileInputStream;
  2. import java.util.Enumeration;
  3. import java.util.Properties;
  4.  
  5. class PropTest {
  6.     public static void main(String[] args) {
  7. Properties pps=new Properties();
  8.     try {
  9.         pps.load(new FileInputStream("config.ini"));
  10.         Enumeration enum1 = pps.propertyNames();
  11.         while (enum1.hasMoreElements()) {
  12.        String strKey = (String) enum1.nextElement();
  13.        String strValue = pps.getProperty(strKey);
  14.        System.out.println(strKey + "=" + strValue);
  15.         }
  16.     } catch (Exception e) {
  17.         e.printStackTrace();
  18.     }
  19.     }
  20. }

3、另一个操作properties文件的实例
  1. import java.io.BufferedInputStream;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.util.Enumeration;
  8. import java.util.Properties;

  9. public class TestMain {

  10.     //根据key读取value
  11.     public static String readValue(String filePath,String key) {
  12.         Properties props = new Properties();

  13.         try {
  14.             InputStream in = new BufferedInputStream (new FileInputStream(filePath));
  15.             props.load(in);
  16.             String value = props.getProperty (key);
  17.             System.out.println(key+value);
  18.             return value;
  19.         } catch (Exception e) {
  20.             e.printStackTrace();
  21.             return null;
  22.         }
  23.     }

  24.     //读取properties的全部信息
  25.     public static void readProperties(String filePath) {
  26.         Properties props = new Properties();

  27.         try {
  28.             InputStream in = new BufferedInputStream (new FileInputStream(filePath));
  29.             props.load(in);
  30.             Enumeration en = props.propertyNames();
  31.             while (en.hasMoreElements()) {
  32.                 String key = (String) en.nextElement();
  33.                 String Property = props.getProperty (key);
  34.                 System.out.println(key+Property);
  35.             }
  36.         } catch (Exception e) {
  37.             e.printStackTrace();
  38.         }
  39.     }

  40.     //写入properties信息
  41.     public static void writeProperties(String filePath,String parameterName,String parameterValue) {
  42.         Properties prop = new Properties();

  43.         try {
  44.             InputStream fis = new FileInputStream(filePath);
  45.             //从输入流中读取属性列表(键和元素对)
  46.             prop.load(fis);
  47.             //调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性,强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
  48.             OutputStream fos = new FileOutputStream(filePath);
  49.             prop.setProperty(parameterName, parameterValue);
  50.             //以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流
  51.             prop.store(fos, "Update '" + parameterName + "' value");
  52.         } catch (IOException e) {
  53.             System.err.println("Visit "+filePath+" for updating "+parameterName+" value error");
  54.         }
  55.     }

  56.     public static void main(String[] args) {
  57.         readValue("info.properties","url");
  58.         writeProperties("info.properties","age","21");
  59.         readProperties("info.properties" );
  60.         System.out.println("OK");
  61.     }
  62. }

阅读(8223) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~