直接上代码
/**
* 根据Key读取Value
* @param filePath
* @param key
* @return
*/
public static String getValueByKey(String filePath,String key){
Properties properties = new Properties();
try {
InputStream inputStream = PropertiesUtil.class.getClassLoader().getResourceAsStream(filePath);
properties.load(inputStream);
String value = properties.getProperty(key);
return value;
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 读取prooerties的全部信息
* @param filePath
* @param key
* @return
*/
public static Map<String,String> getAllProperties(String filePath, String key){
Properties properties = new Properties();
Map<String,String> hashMap = new HashMap<String, String>();
try {
InputStream inputStream = PropertiesUtil.class.getClassLoader().getResourceAsStream(filePath);
properties.load(inputStream);
Enumeration en = properties.propertyNames();//得到配置文件名称
while (en.hasMoreElements()){
String strKey = (String)en.nextElement();
String value = properties.getProperty(strKey);
hashMap.put(strKey,value);
}
return hashMap;
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
//写入信息
public static void writeProperties(String filePath,String key,String value) throws IOException {
Properties properties = new Properties();
InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream(filePath);
//从输入流中读取属性列表
properties.load(in);
String aa = PropertiesUtil.class.getClassLoader().getResource(filePath).getPath();
OutputStream out = new FileOutputStream(filePath);
properties.setProperty(key,value);
properties.store(out,"Update"+key+"name");
}
public static void main(String[] args){
String value =getValueByKey("jpush.properties", "jpush.appKey");
System.out.println(value+"---------------");
}
直接上代码