1. 在Java中获取 .properties 文件的路径 (src/main/resources 下)
ProjectName
|---src/main/java
|---src/main/resources
|---test.properties
packagexxx.yyy;public classUtils {private String filePath = Utils.class.getClassLoader().getResource("test.properties").getPath();
}
2. 获取 .properties Key所对应的值
publicString getPropertyConfig(String key) {
Resource resource= new ClassPathResource("test.properties");
Properties props= null;try{
props=PropertiesLoaderUtils.loadProperties(resource);
}catch(IOException e) {
e.printStackTrace();
}returnprops.getProperty(key);
}
3. 第二种获取 .properties Key 对应值方法
public staticString getValueByKey(String key, String filePath) {
Properties pps= newProperties();try{
InputStream in= new BufferedInputStream (newFileInputStream(filePath));
pps.load(in);
String value=pps.getProperty(key);
System.out.println(key+ " = " +value);returnvalue;
}catch(IOException e) {
e.printStackTrace();return null;
}
}
4. 写入修改 .properties 健值对方法 [单健]
public static void WriteProperties (String filePath, String pKey, String pValue) throwsIOException {
Properties pps= newProperties();
InputStream in=