java的property配置文件的用法

Code:
  1. import java.io.FileInputStream;  
  2. import java.io.FileNotFoundException;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.util.Properties;  
  6.   
  7. /** */  
  8. /** 
  9.  * 读取properties文件 
  10.  *  
  11.  * @author Qutr 
  12.  *  
  13.  */  
  14. public class Configuration {  
  15.     private Properties propertie;  
  16.     private FileInputStream inputFile;  
  17.     private FileOutputStream outputFile;  
  18.   
  19.     /** */  
  20.     /** 
  21.      * 初始化Configuration类 
  22.      */  
  23.     public Configuration() {  
  24.         propertie = new Properties();  
  25.     }  
  26.   
  27.     /** */  
  28.     /** 
  29.      * 初始化Configuration类 
  30.      *  
  31.      * @param filePath 
  32.      *            要读取的配置文件的路径+名称 
  33.      */  
  34.     public Configuration(String filePath) {  
  35.         propertie = new Properties();  
  36.         try {  
  37.             inputFile = new FileInputStream(filePath);  
  38.             propertie.load(inputFile);  
  39.             inputFile.close();  
  40.         } catch (FileNotFoundException ex) {  
  41.             System.out.println("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");  
  42.             ex.printStackTrace();  
  43.         } catch (IOException ex) {  
  44.             System.out.println("装载文件--->失败!");  
  45.             ex.printStackTrace();  
  46.         }  
  47.     }// end ReadConfigInfo(...)  
  48.   
  49.     /** */  
  50.     /** 
  51.      * 重载函数,得到key的值 
  52.      *  
  53.      * @param key 
  54.      *            取得其值的键 
  55.      * @return key的值 
  56.      */  
  57.     public String getValue(String key) {  
  58.         if (propertie.containsKey(key)) {  
  59.             String value = propertie.getProperty(key);// 得到某一属性的值  
  60.             return value;  
  61.         } else  
  62.             return "";  
  63.     }// end getValue()  
  64.   
  65.     /** */  
  66.     /** 
  67.      * 重载函数,得到key的值 
  68.      *  
  69.      * @param fileName 
  70.      *            properties文件的路径+文件名 
  71.      * @param key 
  72.      *            取得其值的键 
  73.      * @return key的值 
  74.      */  
  75.     public String getValue(String fileName, String key) {  
  76.         try {  
  77.             String value = "";  
  78.             inputFile = new FileInputStream(fileName);  
  79.             propertie.load(inputFile);  
  80.             inputFile.close();  
  81.             if (propertie.containsKey(key)) {  
  82.                 value = propertie.getProperty(key);  
  83.                 return value;  
  84.             } else  
  85.                 return value;  
  86.         } catch (FileNotFoundException e) {  
  87.             e.printStackTrace();  
  88.             return "";  
  89.         } catch (IOException e) {  
  90.             e.printStackTrace();  
  91.             return "";  
  92.         } catch (Exception ex) {  
  93.             ex.printStackTrace();  
  94.             return "";  
  95.         }  
  96.     }// end getValue()  
  97.   
  98.     /** */  
  99.     /** 
  100.      * 清除properties文件中所有的key和其值 
  101.      */  
  102.     public void clear() {  
  103.         propertie.clear();  
  104.     }// end clear();  
  105.   
  106.     /** */  
  107.     /** 
  108.      * 改变或添加一个key的值,当key存在于properties文件中时该key的值被value所代替, 当key不存在时,该key的值是value 
  109.      *  
  110.      * @param key 
  111.      *            要存入的键 
  112.      * @param value 
  113.      *            要存入的值 
  114.      */  
  115.     public void setValue(String key, String value) {  
  116.         propertie.setProperty(key, value);  
  117.     }// end setValue()  
  118.   
  119.     /** */  
  120.     /** 
  121.      * 将更改后的文件数据存入指定的文件中,该文件可以事先不存在。 
  122.      *  
  123.      * @param fileName 
  124.      *            文件路径+文件名称 
  125.      * @param description 
  126.      *            对该文件的描述 
  127.      */  
  128.     public void saveFile(String fileName, String description) {  
  129.         try {  
  130.             outputFile = new FileOutputStream(fileName);  
  131.             propertie.store(outputFile, description);  
  132.             outputFile.close();  
  133.         } catch (FileNotFoundException e) {  
  134.             e.printStackTrace();  
  135.         } catch (IOException ioe) {  
  136.             ioe.printStackTrace();  
  137.         }  
  138.     }// end saveFile()  
  139.   
  140.     public static void main(String[] args) {  
  141.         Configuration rc = new Configuration("jin.properties");// 相对路径  
  142.   
  143.         String ip = rc.getValue("ipp");// 以下读取properties文件的值  
  144.         String host = rc.getValue("host");  
  145.         String tab = rc.getValue("tab");  
  146.   
  147.         System.out.println("ip = " + ip + "ip-test leng = "  
  148.                 + "ip-test".length());// 以下输出properties读出的值  
  149.         System.out.println("ip's length = " + ip.length());  
  150.         System.out.println("host = " + host);  
  151.         System.out.println("tab = " + tab);  
  152.         Configuration cf = new Configuration();  
  153.         String ipp = cf.getValue("jin.properties""ip");  
  154.         System.out.println("ipp = " + ipp);  
  155.         // cf.clear();  
  156.         cf.setValue("min""999");  
  157.         cf.setValue("max""1000");  
  158.         cf.saveFile("jin.properties""test");  
  159.   
  160.         // Configuration saveCf = new Configuration();  
  161.         // saveCf.setValue("min", "10");  
  162.         // saveCf.setValue("max", "1000");  
  163.         // saveCf.saveFile("./config/save.perperties");  
  164.   
  165.     }// end main()  
  166.   
  167. }// end class ReadConfigInfo  

http://java.chinaitlab.com/base/748491.html

——————————————————————————————————————

简化版读取property文件代码:

Code:
  1. import java.io.IOException;  
  2. import java.io.UnsupportedEncodingException;  
  3. import java.util.Properties;  
  4.   
  5. public class GetProp {  
  6.     public void GetP() {  
  7.         Properties props = new Properties();  
  8.         try {  
  9.             // 资源文件存放在类文件的根目录下。即是放在src下面。  
  10.             props.load(getClass().getClassLoader().getResourceAsStream(  
  11.                     "GetProp.properties"));  
  12.             // 当资源文件中有中文的时候可以采用下面的编码转化方法来读取。  
  13.             // 或者使用native2ascii jin1.properties或者jin1.txt  
  14.             // jin.properties将资源文件进行编码转化,  
  15.             // 然后直接读取props.getProperty("name");  
  16.             System.out.println(new String(props.getProperty("name").getBytes(  
  17.                     "ISO-8859-1"), "GBK"));  
  18.         } catch (UnsupportedEncodingException e) {  
  19.             // TODO Auto-generated catch block  
  20.             e.printStackTrace();  
  21.         } catch (IOException e) {  
  22.             // TODO Auto-generated catch block  
  23.             e.printStackTrace();  
  24.         }  
  25.     }  
  26.   
  27.     public static void main(String[] args) {  
  28.         GetProp a = new GetProp();  
  29.         a.GetP();  
  30.     }  
  31. }  

 ——————————————————————————————————————————————————————

自我总结了一个读取和写入property文件的例子:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Map;
import java.util.Properties;

/**
 * @author zxw
 *
 */
public class GetSaveProp {
    // public void GetP() {
    // Properties props = new Properties();
    // try {
    // // 资源文件存放在类文件的根目录下。即是放在src下面。
    // props.load(getClass().getClassLoader().getResourceAsStream(
    // "GetProp.properties"));
    // // 当资源文件中有中文的时候可以采用下面的编码转化方法来读取。
    // // 或者使用native2ascii jin1.properties或者jin1.txt
    // // jin.properties将资源文件进行编码转化,
    // // 然后直接读取props.getProperty("name");
    // System.out.println(new String(props.getProperty("name").getBytes(
    // "ISO-8859-1"), "GBK"));
    // } catch (UnsupportedEncodingException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // } catch (IOException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    // }

    private void saveProperty() {
        // 保存文件
        Properties propertie = new Properties();
        String characterString = "1中国";
        propertie.setProperty("character", characterString);
        propertie.setProperty("date", new Date().toString());

        String fileName = "savetest.properties";
        String description = "CharaterTest";
        try {
            FileOutputStream outputFile = new FileOutputStream(fileName);
            propertie.store(outputFile, description);// property类关键的store方法
            outputFile.close();
            // propertie.list(System.out);
            System.out.println("File was saved!");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    public static void main(String[] args) {
         new GetSaveProp().saveProperty();//save file

        // read from property
        Properties readProps = new Properties();
        FileInputStream inStream;
        try {
            inStream = new FileInputStream("savetest.properties");
            readProps.load(inStream);// read from fileinputStream
            // props.list(System.out);
            if (readProps.get("character") != null) {
                System.out.println("character="
                        + new String(readProps.getProperty("character")
                                .getBytes("ISO-8859-1"), "UTF-8"));
                System.out.println("character="
                        + new String(readProps.getProperty("character")
                                .getBytes("UTF-8"), "UTF-8"));

            } else {
                System.out.println(readProps.get("character"));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

——————————————————————————

http://yuanyuan7891.javaeye.com/blog/835595

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值