java background reading configuration file

I encountered a problem during development a few days ago. When reading the configuration file in the background, the attribute value could not be read, so I checked it online, and now I will share it with you here.

Let’s attach the code first:



package com.shafei.util;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;
import org.apache.log4j.Logger;
public class PropertieUtil {
private static Logger logger = Logger.getLogger(PropertieUtil.class);
private PropertieUtil() {
}
/**
* Read a property of the configuration file
*/
public static String readValue(String filePath, String key) {
Properties props = new Properties();
try {
// Note that the path starts with /, otherwise process
if (!filePath.startsWith("/"))
filePath = "/" + filePath;
InputStream in = PropertieUtil.class.getResourceAsStream(filePath);
props.load(in );
String value = props.getProperty(key);
return value;
} catch (Exception e) {
logger.error(e);
return null;
}
}
/**
* Print the entire content of the configuration file (filePath, configuration file name, If there is a path, props/test.properties)
*/
public static void readProperties(String filePath) {
Properties props = new Properties();
try {
// Note that the path starts with /, if not, process
if (!filePath.startsWith(" /"))
filePath = "/" + filePath;
InputStream in = PropertieUtil.class.getResourceAsStream(filePath);
props.load(in);
Enumeration<?> en = props.propertyNames();
// Traverse and print
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = props.getProperty(key);
System.out.println(Property);
logger.info(key + ":" + Property);
}
} catch (Exception e) {
logger.error( e);
}
}
/**
* Write the value to the configuration file
*/
public static void writeProperties(String fileName, String parameterName, String parameterValue) throws Exception {
// Special attention for local testing, if it is a maven project, please go to \target View the file in the directory, not the source code
// Note that the path cannot be added with /, and if it is added, remove it
if (fileName.startsWith("/"))
fileName.substring(1);
String filePath = PropertieUtil.class.getResource("/").getPath()+fileName;
// 获取配置文件
Properties pps = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
pps.load(in);
in.close();
OutputStream out = new FileOutputStream(filePath);
// 设置配置名称和值
pps.setProperty(parameterName, parameterValue);
// comments 等于配置文件的注释
pps.store(out, "Update " + parameterName + " name");
out.flush();
out.close();
}
public static void main(String[] args) throws Exception {
readProperties("jdbc.properties");
logger.info(readValue("jdbc.properties", "JAVABLOG_WRITE_URL"));
// writeProperties("conf/test.properties", "dataSource.driver", "test");
readProperties("conf/test.properties" );
}
}

I also learned the above code from the big cows, it is not difficult, it is very practical, I hope it will be helpful to everyone!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326815429&siteId=291194637