java的property配置文件的用法

  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.     /** *//**     
  19.      * 初始化Configuration类     
  20.      */      
  21.     public Configuration()       
  22.     ...{       
  23.         propertie = new Properties();       
  24.     }       
  25.            
  26.     /** *//**     
  27.      * 初始化Configuration类     
  28.      * @param filePath 要读取的配置文件的路径+名称     
  29.      */      
  30.     public Configuration(String filePath)       
  31.     ...{       
  32.         propertie = new Properties();       
  33.         try ...{       
  34.             inputFile = new FileInputStream(filePath);       
  35.             propertie.load(inputFile);       
  36.             inputFile.close();       
  37.         } catch (FileNotFoundException ex) ...{       
  38.             System.out.println("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");       
  39.             ex.printStackTrace();       
  40.         } catch (IOException ex) ...{       
  41.             System.out.println("装载文件--->失败!");       
  42.             ex.printStackTrace();       
  43.         }       
  44.     }//end ReadConfigInfo(...)       
  45.            
  46.     /** *//**     
  47.      * 重载函数,得到key的值     
  48.      * @param key 取得其值的键     
  49.      * @return key的值     
  50.      */      
  51.     public String getValue(String key)       
  52.     ...{       
  53.         if(propertie.containsKey(key))...{       
  54.             String value = propertie.getProperty(key);//得到某一属性的值       
  55.             return value;       
  56.         }       
  57.         else        
  58.             return "";       
  59.     }//end getValue(...)       
  60.            
  61.     /** *//**     
  62.      * 重载函数,得到key的值     
  63.      * @param fileName properties文件的路径+文件名     
  64.      * @param key 取得其值的键     
  65.      * @return key的值     
  66.      */      
  67.     public String getValue(String fileName, String key)       
  68.     ...{       
  69.         try ...{       
  70.             String value = "";       
  71.             inputFile = new FileInputStream(fileName);       
  72.             propertie.load(inputFile);       
  73.             inputFile.close();       
  74.             if(propertie.containsKey(key))...{       
  75.                 value = propertie.getProperty(key);       
  76.                 return value;       
  77.             }else      
  78.                 return value;       
  79.         } catch (FileNotFoundException e) ...{       
  80.             e.printStackTrace();       
  81.             return "";       
  82.         } catch (IOException e) ...{       
  83.             e.printStackTrace();       
  84.             return "";       
  85.         } catch (Exception ex) ...{       
  86.             ex.printStackTrace();       
  87.             return "";       
  88.         }       
  89.     }//end getValue(...)       
  90.            
  91.     /** *//**     
  92.      * 清除properties文件中所有的key和其值     
  93.      */      
  94.     public void clear()       
  95.     ...{       
  96.         propertie.clear();       
  97.     }//end clear();         
  98.     /** *//**     
  99.      * 改变或添加一个key的值,当key存在于properties文件中时该key的值被value所代替,     
  100.      * 当key不存在时,该key的值是value     
  101.      * @param key 要存入的键     
  102.      * @param value 要存入的值     
  103.      */      
  104.     public void setValue(String key, String value)       
  105.     ...{       
  106.         propertie.setProperty(key, value);       
  107.     }//end setValue(...)       
  108.            
  109.     /** *//**     
  110.      * 将更改后的文件数据存入指定的文件中,该文件可以事先不存在。     
  111.      * @param fileName 文件路径+文件名称     
  112.      * @param description 对该文件的描述     
  113.      */      
  114.     public void saveFile(String fileName, String description)       
  115.     ...{       
  116.         try ...{       
  117.             outputFile = new FileOutputStream(fileName);       
  118.             propertie.store(outputFile, description);       
  119.             outputFile.close();       
  120.         } catch (FileNotFoundException e) ...{       
  121.             e.printStackTrace();       
  122.         } catch (IOException ioe)...{       
  123.             ioe.printStackTrace();       
  124.         }       
  125.     }//end saveFile(...)       
  126.            
  127.     public static void main(String[] args)       
  128.     ...{       
  129.         Configuration rc = new Configuration("./config/test.properties");//相对路径       
  130.                
  131.         String ip = rc.getValue("ipp");//以下读取properties文件的值       
  132.         String host = rc.getValue("host");       
  133.         String tab = rc.getValue("tab");       
  134.                
  135.         System.out.println("ip = " + ip + "ip-test leng = " + "ip-test".length());//以下输出properties读出的值       
  136.         System.out.println("ip's length = " + ip.length());       
  137.         System.out.println("host = " + host);       
  138.         System.out.println("tab = " + tab);       
  139.         Configuration cf = new Configuration();       
  140.         String ipp = cf.getValue("./config/test.properties""ip");       
  141.         System.out.println("ipp = " + ipp);       
  142. //        cf.clear();       
  143.         cf.setValue("min""999");       
  144.         cf.setValue("max""1000");       
  145.         cf.saveFile("./config/save.perperties""test");       
  146.                
  147. //        Configuration saveCf = new Configuration();       
  148. //        saveCf.setValue("min", "10");       
  149. //        saveCf.setValue("max", "1000");       
  150. //        saveCf.saveFile("./config/save.perperties");       
  151.                
  152.     }//end main()       
  153.            
  154. }package configuration;       
  155. import Java.io.FileInputStream;       
  156. import Java.io.FileNotFoundException;       
  157. import Java.io.FileOutputStream;       
  158. import Java.io.IOException;       
  159. import Java.util.Properties;       
  160. /** *//**     
  161. * 读取properties文件     
  162. * @author Qutr     
  163. *     
  164. */      
  165. public class Configuration       
  166. ...{       
  167.     private Properties propertie;       
  168.     private FileInputStream inputFile;       
  169.     private FileOutputStream outputFile;       
  170.            
  171.     /** *//**     
  172.      * 初始化Configuration类     
  173.      */      
  174.     public Configuration()       
  175.     ...{       
  176.         propertie = new Properties();       
  177.     }       
  178.            
  179.     /** *//**     
  180.      * 初始化Configuration类     
  181.      * @param filePath 要读取的配置文件的路径+名称     
  182.      */      
  183.     public Configuration(String filePath)       
  184.     ...{       
  185.         propertie = new Properties();       
  186.         try ...{       
  187.             inputFile = new FileInputStream(filePath);       
  188.             propertie.load(inputFile);       
  189.             inputFile.close();       
  190.         } catch (FileNotFoundException ex) ...{       
  191.             System.out.println("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");       
  192.             ex.printStackTrace();       
  193.         } catch (IOException ex) ...{       
  194.             System.out.println("装载文件--->失败!");       
  195.             ex.printStackTrace();       
  196.         }       
  197.     }//end ReadConfigInfo(...)       
  198.            
  199.     /** *//**     
  200.      * 重载函数,得到key的值     
  201.      * @param key 取得其值的键     
  202.      * @return key的值     
  203.      */      
  204.     public String getValue(String key)       
  205.     ...{       
  206.         if(propertie.containsKey(key))...{       
  207.             String value = propertie.getProperty(key);//得到某一属性的值       
  208.             return value;       
  209.         }       
  210.         else        
  211.             return "";       
  212.     }//end getValue(...)       
  213.            
  214.     /** *//**     
  215.      * 重载函数,得到key的值     
  216.      * @param fileName properties文件的路径+文件名     
  217.      * @param key 取得其值的键     
  218.      * @return key的值     
  219.      */      
  220.     public String getValue(String fileName, String key)       
  221.     ...{       
  222.         try ...{       
  223.             String value = "";       
  224.             inputFile = new FileInputStream(fileName);       
  225.             propertie.load(inputFile);       
  226.             inputFile.close();       
  227.             if(propertie.containsKey(key))...{       
  228.                 value = propertie.getProperty(key);       
  229.                 return value;       
  230.             }else      
  231.                 return value;       
  232.         } catch (FileNotFoundException e) ...{       
  233.             e.printStackTrace();       
  234.             return "";       
  235.         } catch (IOException e) ...{       
  236.             e.printStackTrace();       
  237.             return "";       
  238.         } catch (Exception ex) ...{       
  239.             ex.printStackTrace();       
  240.             return "";       
  241.         }       
  242.     }//end getValue(...)       
  243.            
  244.     /** *//**     
  245.      * 清除properties文件中所有的key和其值     
  246.      */      
  247.     public void clear()       
  248.     ...{       
  249.         propertie.clear();       
  250.     }//end clear();         
  251.     /** *//**     
  252.      * 改变或添加一个key的值,当key存在于properties文件中时该key的值被value所代替,     
  253.      * 当key不存在时,该key的值是value     
  254.      * @param key 要存入的键     
  255.      * @param value 要存入的值     
  256.      */      
  257.     public void setValue(String key, String value)       
  258.     ...{       
  259.         propertie.setProperty(key, value);       
  260.     }//end setValue(...)       
  261.            
  262.     /** *//**     
  263.      * 将更改后的文件数据存入指定的文件中,该文件可以事先不存在。     
  264.      * @param fileName 文件路径+文件名称     
  265.      * @param description 对该文件的描述     
  266.      */      
  267.     public void saveFile(String fileName, String description)       
  268.     ...{       
  269.         try ...{       
  270.             outputFile = new FileOutputStream(fileName);       
  271.             propertie.store(outputFile, description);       
  272.             outputFile.close();       
  273.         } catch (FileNotFoundException e) ...{       
  274.             e.printStackTrace();       
  275.         } catch (IOException ioe)...{       
  276.             ioe.printStackTrace();       
  277.         }       
  278.     }//end saveFile(...)       
  279.            
  280.     public static void main(String[] args)       
  281.     ...{       
  282.         Configuration rc = new Configuration("./config/test.properties");//相对路径       
  283.                
  284.         String ip = rc.getValue("ipp");//以下读取properties文件的值       
  285.         String host = rc.getValue("host");       
  286.         String tab = rc.getValue("tab");       
  287.                
  288.         System.out.println("ip = " + ip + "ip-test leng = " + "ip-test".length());//以下输出properties读出的值       
  289.         System.out.println("ip's length = " + ip.length());       
  290.         System.out.println("host = " + host);       
  291.         System.out.println("tab = " + tab);       
  292.         Configuration cf = new Configuration();       
  293.         String ipp = cf.getValue("./config/test.properties""ip");       
  294.         System.out.println("ipp = " + ipp);       
  295. //        cf.clear();       
  296.         cf.setValue("min""999");       
  297.         cf.setValue("max""1000");       
  298.         cf.saveFile("./config/save.perperties""test");       
  299.                
  300. //        Configuration saveCf = new Configuration();       
  301. //        saveCf.setValue("min", "10");       
  302. //        saveCf.setValue("max", "1000");       
  303. //        saveCf.saveFile("./config/save.perperties");       
  304.                
  305.     }//end main()       
  306.            
  307. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值