【Properties文件】Java使用Properties来读取配置文件

配置文件位置及内容

2.png
1.png


3.png
4.png
5.png
6.png

7.png


执行结果

9.png


程序代码


  1. package Utils.ConfigFile;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedReader;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.util.Enumeration;
  10. import java.util.Iterator;
  11. import java.util.Map;
  12. import java.util.Properties;
  13.  
  14. public class PropertiesTest {
  15.  
  16.     /**
  17.      * 获取属性文件
  18.      * @param path  属性文件路径
  19.      * @return
  20.      */
  21.     public static Properties getPropsFile(String path) {
  22.         Properties props = new Properties();
  23.         try {
  24.             File file = new File(path);
  25.             InputStream in = new BufferedInputStream(new FileInputStream(file));
  26.  
  27.             //解决中午乱码问题--因为字节流无法读取中文,所以采用reader把inputStream转换成reader用字符流来读取中文
  28.             BufferedReader bf = new BufferedReader(new InputStreamReader(in));
  29.             props.load(bf);
  30.             in.close();
  31.         } catch (Exception e) {
  32.             return null;
  33.         }
  34.         return props;
  35.     }
  36.  
  37.     /**
  38.      * 显示所有键值
  39.      * @param properties
  40.      */
  41.     public static void showKeys(Properties properties){
  42.         Enumeration<?> enumeration = properties.propertyNames();
  43.  
  44.         System.out.println("======下面将显示所有key值============");
  45.         while(enumeration.hasMoreElements()){
  46.             Object key = enumeration.nextElement();
  47.             System.out.println(key);
  48.         }
  49.     }
  50.  
  51.     /**
  52.      * 显示所有value值
  53.      * @param properties
  54.      */
  55.     public static void showValues(Properties properties){
  56.         Enumeration<?> enumeration = properties.elements();
  57.  
  58.         System.out.println("======下面将显示所有value值============");
  59.         while(enumeration.hasMoreElements()){
  60.             Object value = enumeration.nextElement();
  61.             System.out.println(value);
  62.         }
  63.     }
  64.  
  65.     /**
  66.      * 显示所有key,value
  67.      * @param properties
  68.      */
  69.     public static void showKeysAndValues(Properties properties){
  70.         Iterator<Map.Entry<Object, Object>> it= properties.entrySet().iterator();
  71.  
  72.         System.out.println("======下面将显示所有<key,value>值---方式1============");
  73.         while (it.hasNext()) {
  74.             Map.Entry<Object, Object> entry = it.next();
  75.             Object key = entry.getKey().toString();
  76.             Object value = entry.getValue();
  77.             System.out.println("<" + key + "," + value + ">");
  78.         }
  79.     }
  80.  
  81.     /**
  82.      * 显示所有key,value
  83.      * @param properties
  84.      */
  85.     public static void showKeysAndValues2(Properties properties){
  86.         System.out.println("======下面将显示所有<key,value>值--方式2============");
  87.         for (Map.Entry<Object, Object> entry: properties.entrySet()) {
  88.             Object key = entry.getKey();
  89.             Object value = entry.getValue();
  90.             System.out.println("<" + key + "," + value + ">");
  91.         }
  92.     }
  93.  
  94.     public static void main(String args[]) {
  95.         Properties propFile = getPropsFile("C:\myProperties.properties");
  96.  
  97.         showKeys(propFile);
  98.         showValues(propFile);
  99.         showKeysAndValues(propFile);
  100.         showKeysAndValues2(propFile);
  101.     }
  102. }

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 









原文地址:https://www.cnblogs.com/ssslinppp/p/5069029.html