Java的Properties配置文件用法【续】 .

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值