properties便捷读取
-
ResourceBundle
ResourceBundle res = ResourceBundle.getBundle("ipConfig"); String testIP = res.getString("testIP");
-
spring注解
//全局变量 @Value("${testIP}") private String testIP;
properties
-
读取
private String readPropertiesValue(String path, String key) { Properties prop = new Properties(); String url = String.valueOf(String.class.getClassLoader().getResource("")); InputStream in = null; try { in = new BufferedInputStream(new FileInputStream(url.split("file:/")[1].split("WEB-INF")[0] + path)); //读取属性文件a.properties prop.load(in); ///加载属性列表 Iterator<String> it = prop.stringPropertyNames().iterator(); while (it.hasNext()) { String temp = it.next(); if (key.equals(temp)) { return prop.getProperty(temp); } } } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != in) { in.close(); } } catch (IOException e) { e.printStackTrace(); } } return null; }
2.写入 (只修改一个key,value)
private static void writeProperties(String path, String key, String value) { Properties prop = new Properties(); String url = String.valueOf(String.class.getClassLoader().getResource("")); InputStream in = null; FileOutputStream out = null; try { in = new BufferedInputStream(new FileInputStream(url.split("file:/")[1].split("WEB-INF")[0] + path)); //读取属性文件a.properties prop.load(in); ///加载属性列表 out = new FileOutputStream((url.split("file:/")[1].split("WEB-INF")[0] + path)); Iterator<String> it = prop.stringPropertyNames().iterator(); while (it.hasNext()) { String temp = it.next(); if (!kay.equals(temp)) { prop.setProperty(temp, prop.getProperty(temp)); } } prop.setProperty(key, value); prop.store(out, (url.split("file:/")[1].split("WEB-INF")[0] + path)); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != out) { out.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (null != in) { in.close(); } } catch (IOException e) { e.printStackTrace(); } }
}
.json
-
读取
public static JSONObject getJSONObject(String path){ String url=String.valueOf(String.class.getClassLoader().getResource("")); String path = url.split("file:/")[1].split("WEB-INF")[0]+path; BufferedReader reader = null; String jsonStr = ""; try { FileInputStream fileInputStream = new FileInputStream(path); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8"); reader = new BufferedReader(inputStreamReader); String tempString = null; while ((tempString = reader.readLine()) != null) { jsonStr += tempString; } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } if(null==jsonStr||"".equals(jsonStr)){ return null; } return JSONObject.fromObject(jsonStr);
}
2.写入
public static void writeJson(String path, JSONObject json){
OutputStreamWriter out = null;
String url=String.valueOf(JsonUtil.class.getClassLoader().getResource(""));
String path = url.split("file:/")[1].split("WEB-INF")[0]+name;
try {
File file = new File(path);
if (!file.exists()) {
file.createNewFile();
}
out = new OutputStreamWriter(new FileOutputStream(path),"utf-8");
out.write(json.toString());
out.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}