zoukankan      html  css  js  c++  java
  • java读取properties

      1 package com.avonaco.web.excel;  
      2   
      3 import java.io.BufferedInputStream;  
      4 import java.io.FileInputStream;  
      5 import java.io.InputStream;  
      6 import java.util.Enumeration;  
      7 import java.util.HashMap;  
      8 import java.util.Map;  
      9 import java.util.Properties;  
     10 import org.apache.commons.logging.Log;  
     11 import org.apache.commons.logging.LogFactory;  
     12   
     13 public class PropertiesUtil {  
     14       
     15     private static final Log log = LogFactory.getLog(PropertiesUtil.class);  
     16   
     17     private static Properties properties = new Properties() ;  
     18       
     19     public static final String US = "ApplicationResources.properties";          //英文  
     20   
     21     public static final String CHINA = "ApplicationResources_zh_CN.properties"; //中文  
     22       
     23     public static final String CHINA_USER = "ApplicationResourcesUserManager_zh_CN.properties" ;  
     24       
     25     public static final String US_USER = "ApplicationResourcesUserManager.properties";  
     26     /** 
     27      * @desc 取得全部的key和value 
     28      * @param propertiesName 
     29      * @return 
     30      * @author:  lic 
     31      * @datetime:2011-12-14 上午9:50:21 
     32      */  
     33     @SuppressWarnings({ "rawtypes", "unchecked" })  
     34     public static Map getProperties( String propertiesName){  
     35           
     36         Map map = new HashMap();   
     37           
     38         if ( propertiesName != null && !propertiesName.equals("") ){  
     39             String  url = PropertiesUtil.getPropertiesPath(propertiesName);  
     40             if( log.isDebugEnabled() ){  
     41                 log.debug(">>>>>>>>>>>>>>>>>>>获取Properties文件路径-- " + url);  
     42             }  
     43               
     44             try {  
     45                 InputStream in =new BufferedInputStream(new FileInputStream(url));  
     46                 PropertiesUtil.properties.load(in);  
     47                   
     48                 System.out.println("1");  
     49                   Enumeration en = PropertiesUtil.properties.propertyNames();  
     50                    while (en.hasMoreElements()) {  
     51                        String key = (String) en.nextElement();  
     52                        String value = PropertiesUtil.properties.getProperty(key);  
     53                      map.put(key, value);  
     54                     System.out.println();  
     55                     if( log.isDebugEnabled()){  
     56                         log.debug(">>>>>>>>>>>>>>>>>>>>>>>>>>  " + propertiesName + " -- : " + key  + " >>> value = " + value);  
     57                     }  
     58                    }  
     59             } catch (Exception e) {  
     60                 log.debug(">>>>>>>> PropertiesUtil取得全部的key和value出错  :  " + e);  
     61                 e.printStackTrace();  
     62             }  
     63         }  
     64           
     65           
     66         return map ;  
     67     }  
     68       
     69       
     70       
     71     /** 
     72      * @desc 根据key得到properties的value 
     73      * @param propertiesKey 
     74      * @return Map 
     75      * @author:  lic 
     76      * @datetime:2011-12-14 上午10:02:13 
     77      */  
     78     @SuppressWarnings("rawtypes")  
     79     public static String getPropertiesValue( String propertieskey , String filename){  
     80               
     81             String value = "" ;  
     82               
     83             if ( propertieskey != null && !propertieskey.equals("") ){  
     84                 String pkey = filename;  
     85                 String  url = PropertiesUtil.getPropertiesPath(filename);  
     86                 try {  
     87                     InputStream in =new BufferedInputStream(new FileInputStream(url));  
     88 //                  this.properties.load(in);  
     89                     PropertiesUtil.properties.load(in);  
     90                     System.out.println("1");  
     91 //                    Enumeration en = properties.propertyNames();  
     92                       Enumeration en = PropertiesUtil.properties.propertyNames();  
     93                        while (en.hasMoreElements()) {  
     94                            String key = (String) en.nextElement();  
     95                            if ( key.equals(propertieskey) ){  
     96 //                             value = this.properties.getProperty(key);  
     97                                value = PropertiesUtil.properties.getProperty(key);  
     98                            }  
     99                        }  
    100                 } catch (Exception e) {  
    101                     e.printStackTrace();  
    102                 }  
    103             }  
    104               
    105               
    106             return value ;  
    107         }  
    108           
    109       
    110   
    111     /** 
    112      * @desc 根据properties文件名获取路径 
    113      * @param propertiesKey 
    114      * @return Map 
    115      * @author:  lic 
    116      * @datetime:2011-12-13 下午15:53:58 
    117      */  
    118     public static String getPropertiesPath( String propertiesName ){  
    119               
    120             String path = "";  
    121             if ( propertiesName != null && !propertiesName.equals("") ){  
    122                 path = PropertiesUtil.class.getClassLoader().getResource(propertiesName).toString().substring(6);  
    123             }  
    124               
    125             return path ;  
    126     }  
    127           
    128           
    129 }  
     1 import java.io.BufferedInputStream;
     2 import java.io.FileInputStream;
     3 import java.io.FileOutputStream;
     4 import java.io.IOException;
     5 import java.io.InputStream;
     6 import java.io.OutputStream;
     7 import java.util.Enumeration;
     8 import java.util.Properties;
     9 
    10 public class PropertiesControl {
    11  
    12  //根据key读取value
    13  public static String readValue(String filePath, String key) {
    14   Properties props = new Properties();
    15         try {
    16          InputStream in = new BufferedInputStream (new FileInputStream(filePath));
    17          props.load(in);
    18          String value = props.getProperty (key);
    19             //System.out.println(key+value);
    20             return value;
    21         } catch (Exception e) {
    22          e.printStackTrace();
    23          return null;
    24         }
    25  }
    26  
    27  //读取properties的全部信息
    28     public static void readProperties(String filePath) {
    29      Properties props = new Properties();
    30         try {
    31          InputStream in = new BufferedInputStream (new FileInputStream(filePath));
    32          props.load(in);
    33             Enumeration en = props.propertyNames();
    34              while (en.hasMoreElements()) {
    35               String key = (String) en.nextElement();
    36                     String Property = props.getProperty (key);
    37                     System.out.println(key+Property);
    38                 }
    39         } catch (Exception e) {
    40          e.printStackTrace();
    41         }
    42     }
    43 
    44     //写入properties信息
    45     public static void writeProperties(String filePath,String parameterName,String parameterValue) {
    46      Properties prop = new Properties();
    47      try {
    48       InputStream fis = new FileInputStream(filePath);
    49             //从输入流中读取属性列表(键和元素对)
    50             prop.load(fis);
    51             //调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
    52             //强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
    53             OutputStream fos = new FileOutputStream(filePath);
    54             prop.setProperty(parameterName, parameterValue);
    55             //以适合使用 load 方法加载到 Properties 表中的格式,
    56             //将此 Properties 表中的属性列表(键和元素对)写入输出流
    57             prop.store(fos, "Update '" + parameterName + "' value");
    58         } catch (IOException e) {
    59          System.err.println("Visit "+filePath+" for updating "+parameterName+" value error");
    60         }
    61     }
    62 
    63     public static void main(String[] args) {
    64         readValue("info.properties","url");
    65         writeProperties("info.properties","age","21");
    66         readProperties("info.properties" );
    67         System.out.println("OK");
    68     }
    69 }
    View Code
  • 相关阅读:
    编写第一个 .NET 微服务
    Docker 基础知识编排在开发机上设置和使用 Kubernetes 环境
    将微服务部署到 Azure Kubernetes 服务 (AKS) 实践
    .NET Task.Run vs Task.Factory.StartNew
    Python上下文管理器
    Python可迭代的对象与迭代器的对比
    开发你的第一个SpringBoot应用
    Flask的Blueprints和Views
    Flask项目发布流程
    长篇大论Python生成器
  • 原文地址:https://www.cnblogs.com/sunny-sl/p/7262669.html
Copyright © 2011-2022 走看看