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: getValue14 * @Description: 根据资源目录下的文件名,键,获取对应的值15 * @param resourceFileName 资源目录下的properties文件(resource目录)16 * @param key 键名称17 * @return String 返回value值18 * @throws19 */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: getValue36 * @Description: 读取指定file,根据key获取value37 * @param file38 * @param key39 * @return String 返回类型40 * @throws41 */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: getProPerties57 * @Description: 根据文件系统路径获取Properties对象58 * @param path59 * @return Properties 返回类型60 * @throws61 */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: getProPerties76 * @Description: 根据资源路径下的文件名称获取Properties对象77 * @param path78 * @return Properties 返回类型79 * @throws80 */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 }