Java Properties 类读取配置文件信息在我们平时写程序的时候,有些参数是经常改变的,而这种改变不是我们预知的。java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"的格式,在properties文件中,可以用"#"来作注释,properties文件在Java编程中用到的地方很多,操作很方便。从中可以看到如何读取properties文件,并应用读取出来的值,是学习操作properties文件的好例子。
配置文件config.ini
- 获取JVM的系统属性
- import java.util.Properties;
- class PropTest {
- public static void main(String[] args) {
- Properties pps = System.getProperties();
- pps.list(System.out);
- }
- }
引用
结果:
-- listing properties --
java.runtime.name=Java(TM) 2 Runtime Environment, Stand...
sun.boot.library.path=C:\Program Files\Java\jdk1.5.0_04\jre...
java.vm.version=1.5.0_04-b05
java.vm.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.com/
path.separator=;
java.vm.name=Java HotSpot(TM) Client VM
file.encoding.pkg=sun.io
user.country=CN
sun.os.patch.level=Service Pack 2
java.vm.specification.name=Java Virtual Machine Specification
...以下略
-- listing properties --
java.runtime.name=Java(TM) 2 Runtime Environment, Stand...
sun.boot.library.path=C:\Program Files\Java\jdk1.5.0_04\jre...
java.vm.version=1.5.0_04-b05
java.vm.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.com/
path.separator=;
java.vm.name=Java HotSpot(TM) Client VM
file.encoding.pkg=sun.io
user.country=CN
sun.os.patch.level=Service Pack 2
java.vm.specification.name=Java Virtual Machine Specification
...以下略
- 读取配置文件
配置文件config.ini
- author=ZJ
- user=all
- copyright=2006-2007
- import java.io.FileInputStream;
- import java.util.Enumeration;
- import java.util.Properties;
- class PropTest {
- public static void main(String[] args) {
- Properties pps=new Properties();
- try {
- pps.load(new FileInputStream("config.ini"));
- Enumeration enum1 = pps.propertyNames();
- while (enum1.hasMoreElements()) {
- String strKey = (String) enum1.nextElement();
- String strValue = pps.getProperty(strKey);
- System.out.println(strKey + "=" + strValue);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
引用
结果:
user=all
copyright=2006-2007
author=ZJ
user=all
copyright=2006-2007
author=ZJ
- 另外一个用JAVA操作properties文件的实例
- package control;
- import java.io.BufferedInputStream;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.Enumeration;
- import java.util.Properties;
- public class TestMain {
- //根据key读取value
- public static String readValue(String filePath,String key) {
- Properties props = new Properties();
- try {
- InputStream in = new BufferedInputStream (new FileInputStream(filePath));
- props.load(in);
- String value = props.getProperty (key);
- System.out.println(key+value);
- return value;
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- //读取properties的全部信息
- public static void readProperties(String filePath) {
- Properties props = new Properties();
- try {
- InputStream in = new BufferedInputStream (new FileInputStream(filePath));
- props.load(in);
- Enumeration en = props.propertyNames();
- while (en.hasMoreElements()) {
- String key = (String) en.nextElement();
- String Property = props.getProperty (key);
- System.out.println(key+Property);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- //写入properties信息
- public static void writeProperties(String filePath,String parameterName,String parameterValue) {
- Properties prop = new Properties();
- try {
- InputStream fis = new FileInputStream(filePath);
- //从输入流中读取属性列表(键和元素对)
- prop.load(fis);
- //调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
- //强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
- OutputStream fos = new FileOutputStream(filePath);
- prop.setProperty(parameterName, parameterValue);
- //以适合使用 load 方法加载到 Properties 表中的格式,
- //将此 Properties 表中的属性列表(键和元素对)写入输出流
- prop.store(fos, "Update '" + parameterName + "' value");
- } catch (IOException e) {
- System.err.println("Visit "+filePath+" for updating "+parameterName+" value error");
- }
- }
- public static void main(String[] args) {
- readValue("info.properties","url");
- writeProperties("info.properties","age","21");
- readProperties("info.properties" );
- System.out.println("OK");
- }
- }