Java-properties

 1 import java.io.File;
 2 import java.io.FileInputStream;
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.io.InputStreamReader;
 6 import java.util.Properties;
 7 
 8 public class PropertiesUtil {
 9 
10     private PropertiesUtil() {}
11     
12     /**
13      * @Title: getValue
14      * @Description: 根据资源目录下的文件名,键,获取对应的值
15      * @param resourceFileName 资源目录下的properties文件(resource目录)
16      * @param key  键名称
17      * @return String  返回value值
18      * @throws
19      */
20     public static String getResourceProValue(String resourceFileName,String key) {
21         Properties properties = null;
22         String value = "";
23         try {
24             properties = new Properties();
25             InputStream in = ClassLoader.getSystemResourceAsStream(resourceFileName);
26             properties.load(in);
27             value = properties.getProperty(key);
28         } catch (IOException e) {
29             e.printStackTrace();
30         }
31         return value;
32     }
33     
34     /**
35      * @Title: getValue
36      * @Description: 读取指定file,根据key获取value
37      * @param file
38      * @param key
39      * @return String  返回类型
40      * @throws
41      */
42     public static String getSystemProValue(String systemFilePath,String key) {
43         Properties properties = null;
44         String value = "";
45         try {
46             properties = new Properties();
47             properties.load(new FileInputStream(new File(systemFilePath)));
48             value = properties.getProperty(key);
49         } catch (IOException e) {
50             e.printStackTrace();
51         }
52         return value;
53     }
54     
55     /**
56      * @Title: getProPerties
57      * @Description: 根据文件系统路径获取Properties对象
58      * @param path
59      * @return Properties  返回类型
60      * @throws
61      */
62     public static Properties getSystemProPerties(String systemFilePath) {
63         Properties properties = null;
64         try {
65             properties = new Properties();
66             
67             properties.load(new InputStreamReader(new FileInputStream(new File(systemFilePath)), "UTF-8"));
68         } catch (IOException e) {
69             e.printStackTrace();
70         }
71         return properties;
72     }
73     
74     /**
75      * @Title: getProPerties
76      * @Description: 根据资源路径下的文件名称获取Properties对象
77      * @param path
78      * @return Properties  返回类型
79      * @throws
80      */
81     public static Properties getResourceProperties(String resourceFilePath) {
82         Properties properties = null;
83         try {
84             properties = new Properties();
85             properties.load(ClassLoader.getSystemResourceAsStream(resourceFilePath));
86         } catch (IOException e) {
87             e.printStackTrace();
88         }
89         return properties;
90     }
91 }
原文地址:https://www.cnblogs.com/wang1001/p/9759741.html