---
title: "Java Properties 类读取配置文件信息"
author: "java-admin"
date: "2009-10-24T11:27:03Z"
tags: [".net", "Blog", "JDK", "Java", "Python"]
slug: "java-properties"
draft: false
lastmod: "2009-11-18T15:11:38Z"
id: 503201
url: "https://java-admin.iteye.com/blog/503201"
---

转载：http://blog.csdn.net/DL88250/archive/2008/02/19/2104561.aspx

在我们平时写程序的时候，有些参数是经常改变的，而这种改变不是我们预知的。比如说我们开发了一个操作数据库的模块，在开发的时候我们连接本地的数据库那么 IP ，数据库名称，表名称，数据库主机等信息是我们本地的，要使得这个操作数据的模块具有通用性，那么以上信息就不能写死在程序里。通常我们的做法是用配置文件来解决。

各种语言都有自己所支持的配置文件类型。比如 Python ，他支持 .ini 文件。因为他内部有一个 ConfigParser 类来支持 .ini 文件的读写，根据该类提供的方法程序员可以自由的来操作 .ini 文件。而在 Java 中， Java 支持的是 .properties 文件的读写。 JDK 内置的 java.util.Properties 类为我们操作 .properties 文件提供了便利。

 

**一． .properties 文件的形式** ==========================================================

 

\# 以下为服务器、数据库信息

dbPort = localhost

databaseName = mydb

dbUserName = root

dbPassword = root

\# 以下为数据库表信息

dbTable = mytable

\# 以下为服务器信息

ip = 192.168.0.9

······

在上面的文件中我们假设该文件名为： test.properties 文件。其中 # 开始的一行为注释信息；在等号“ = ”左边的我们称之为 key ；等号“ = ”右边的我们称之为 value 。（其实就是我们常说的键 - 值对） key 应该是我们程序中的变量。而 value 是我们根据实际情况配置的。

 

**二． JDK 中的 Properties 类** Properties 类存在于胞 Java.util 中，该类继承自 `Hashtable``，它提供了几个主要的方法：`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 () ，清除所有装载的`键 - 值对。该方法在基类中提供。

有了以上几个方法我们就可以对 .properties 文件进行操作了！

**三．代码实例**

 

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package  configuration;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) import  java.io.FileInputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) import  java.io.FileNotFoundException;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) import  java.io.FileOutputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) import  java.io.IOException;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) import  java.util.Properties;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif) /\** \*/ /\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  * 读取properties文件  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  *  @author  Qutr  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  *  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)   \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) public   class  Configuration  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      private  Properties propertie;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      private  FileInputStream inputFile;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      private  FileOutputStream outputFile;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)       
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      /\** \*/ /\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      * 初始化Configuration类  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)       \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      public  Configuration()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         propertie  =   new  Properties();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)       
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      /\** \*/ /\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      * 初始化Configuration类  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      *  @param  filePath 要读取的配置文件的路径+名称  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)       \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      public  Configuration(String filePath)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         propertie  =   new  Properties();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)          try   ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             inputFile  =   new  FileInputStream(filePath);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             propertie.load(inputFile);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             inputFile.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)         }   catch  (FileNotFoundException ex)  ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             System.out.println( " 读取属性文件---&gt;失败！- 原因：文件路径错误或者文件不存在 " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             ex.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)         }   catch  (IOException ex)  ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             System.out.println( " 装载文件---&gt;失败! " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             ex.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)         }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     } // end ReadConfigInfo(...)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)       
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      /\** \*/ /\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      * 重载函数，得到key的值  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      *  @param  key 取得其值的键  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      *  @return  key的值  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)       \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      public  String getValue(String key)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)          if (propertie.containsKey(key)) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             String value  =  propertie.getProperty(key); // 得到某一属性的值  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)              return  value;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)         }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          else    
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)              return   "" ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     } // end getValue(...)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)       
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      /\** \*/ /\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      * 重载函数，得到key的值  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      *  @param  fileName properties文件的路径+文件名  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      *  @param  key 取得其值的键  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      *  @return  key的值  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)       \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      public  String getValue(String fileName, String key)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)          try   ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             String value  =   "" ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             inputFile  =   new  FileInputStream(fileName);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             propertie.load(inputFile);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             inputFile.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)              if (propertie.containsKey(key)) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                 value  =  propertie.getProperty(key);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                  return  value;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)             } else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                  return  value;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)         }   catch  (FileNotFoundException e)  ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             e.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)              return   "" ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)         }   catch  (IOException e)  ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             e.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)              return   "" ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)         }   catch  (Exception ex)  ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             ex.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)              return   "" ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)         }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     } // end getValue(...)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)       
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      /\** \*/ /\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      * 清除properties文件中所有的key和其值  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)       \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      public   void  clear()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         propertie.clear();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     } // end clear();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)       
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      /\** \*/ /\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      * 改变或添加一个key的值，当key存在于properties文件中时该key的值被value所代替，  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      * 当key不存在时，该key的值是value  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      *  @param  key 要存入的键  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      *  @param  value 要存入的值  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)       \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      public   void  setValue(String key, String value)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         propertie.setProperty(key, value);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     } // end setValue(...)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)       
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      /\** \*/ /\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      * 将更改后的文件数据存入指定的文件中，该文件可以事先不存在。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      *  @param  fileName 文件路径+文件名称  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      *  @param  description 对该文件的描述  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)       \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      public   void  saveFile(String fileName, String description)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)          try   ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             outputFile  =   new  FileOutputStream(fileName);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             propertie.store(outputFile, description);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             outputFile.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)         }   catch  (FileNotFoundException e)  ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             e.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)         }   catch  (IOException ioe) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             ioe.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)         }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     } // end saveFile(...)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)       
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      public   static   void  main(String\[] args)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         Configuration rc  =   new  Configuration( " .\\config\\test.properties " ); // 相对路径  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         String ip  =  rc.getValue( " ipp " ); // 以下读取properties文件的值  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         String host  =  rc.getValue( " host " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         String tab  =  rc.getValue( " tab " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         System.out.println( " ip =  "   +  ip  +   " ip-test leng =  "   +   " ip-test " .length()); // 以下输出properties读出的值  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         System.out.println( " ip's length =  "   +  ip.length());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         System.out.println( " host =  "   +  host);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         System.out.println( " tab =  "   +  tab);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         Configuration cf  =   new  Configuration();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         String ipp  =  cf.getValue( " .\\config\\test.properties " ,  " ip " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         System.out.println( " ipp =  "   +  ipp);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) //         cf.clear();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         cf.setValue( " min " ,  " 999 " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         cf.setValue( " max " ,  " 1000 " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         cf.saveFile( " .\\config\\save.perperties " ,  " test " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) //         Configuration saveCf = new Configuration();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) //         saveCf.setValue("min", "10");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) //         saveCf.setValue("max", "1000");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) //         saveCf.saveFile(".\\config\\save.perperties");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     } // end main()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)       
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) } // end class ReadConfigInfo  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

四．小结 通过上面的例子不难看出，在 Java 中操作配置文件是非常简单的。在一个需要用到大量配置信息的模块或系统里，我们有必要封装一个专门的类来共使用。通过最后的 main 函数调用，相信大家可以看出该类的用法。不足指出希望大家多多指点。