Java读取properties文件

 1 package com.test;
 2 
 3 import java.util.Properties;
 4 import java.io.InputStream;
 5 import java.io.IOException;
 6 
 7 public final class ReadPropertiesUtil {
 8     private static String param1;
 9     private static String param2;
10 
11     static {
12         Properties prop = new Properties();
13 //        InputStream in1 = Object.class.getResourceAsStream("/com/property/test.properties");
14 //        InputStream in2 = ReadPropertiesUtil.class.getResourceAsStream("/com/property/test.properties");
15         InputStream in3 = ReadPropertiesUtil.class.getClassLoader().getResourceAsStream("com/property/test.properties");  
16         try {
17             prop.load(in3);
18             param1 = prop.getProperty("name").trim();
19             param2 = prop.getProperty("password").trim();
20         } catch (IOException e) {
21             e.printStackTrace();
22         }
23     }
24 
25     /**
26      * 私有构造方法,不需要创建对象
27      */
28     private ReadPropertiesUtil() {
29     }
30 
31     public static String getParam1() {
32         return param1;
33     }
34 
35     public static String getParam2() {
36         return param2;
37     }
38 
39     public static void main(String args[]) {
40         System.out.println(getParam1());
41         System.out.println(getParam2());
42     }
43 }
原文地址:https://www.cnblogs.com/luomsg/p/5027874.html