首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Java Properties 种读取配置文件信息【转】

2012-12-21 
Java Properties 类读取配置文件信息【转】在我们平时写程序的时候,有些参数是经常改变的,而这种改变不是我

Java Properties 类读取配置文件信息【转】

在我们平时写程序的时候,有些参数是经常改变的,而这种改变不是我们预知的。比如说我们开发了一个操作数据库的模块,在开发的时候我们连接本地的数据库那么 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 文件进行操作了!

三.代码实例

?

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

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

?

?Java properties文件的操作?


??????? log.info("获取的icisReportURL=" + icisReportURL);
??????? return icisReportURL;
??? }

------------------------------------------------------

总 结:java的properties文件需要放到classpath下面,这样程序才能读取到,有关classpath实际上就是java类或者库的存放 路径,在java工程中,properties放到class文件一块。在web应用中,最简单的方法是放到web应用的WEB-INF\classes 目录下即可,也可以放在其他文件夹下面,这时候需要在设置classpath环境变量的时候,将这个文件夹路径加到classpath变量中,这样也也可 以读取到。在此,你需要对classpath有个深刻理解,classpath绝非系统中刻意设定的那个系统环境变量,WEB-INF\classes其 实也是,java工程的class文件目录也是。

热点排行