The main class used in the Java operation properties file is the java.util.Properties class.
Load is used to load resource files The getProperty method is used to get the property value The setProperty method is used to set the property value Remove to remove attributes store is used to write attributes
The project structure is as follows:
Where PropertiesUtils.java is the tool class for operating properties. test.properties is the properties file to operate on.
Here is the detailed code of PropertiesUtils class:
public class PropertiesUtils {
/**
* Get the actual path of the file
* @param filePath The path of the file relative to the root
* @return
*/
public static String getRealPath(String filePath){
ClassLoader classLoader=Thread.currentThread().getContextClassLoader();
URL url=classLoader.getResource(filePath);
return url.getPath();
}
/**
* Get Properties object
* @param filePath The path of the file relative to the root
* @return
* @throws IOException
*/
public static Properties getProperties(String filePath) throws IOException{
Properties properties=new Properties();
FileInputStream in=new FileInputStream(getRealPath(filePath));
properties.load(in);
return properties;
}
/**
* Get all non empty key s
* Or properties.stringPropertyNames(): get the set set of key
* Or properties.propertyNames(): get the enumeration of the key
* @param filePath The path of the file relative to the root
* @return Non empty key set
* @throws IOException
*/
public static List<String> getAllKeys(String filePath) throws IOException{
List<String> list=new ArrayList<String>();
Properties props=getProperties(filePath);
Iterator<Object> iterator=props.keySet().iterator();
if(iterator!=null){
while(iterator.hasNext()){
String key=(String) iterator.next();
if(StringUtils.isNotBlank(key) && !list.contains(key)){
list.add(key);
}
}
}
return list;
}
/**
* Get all key value sets
* @param filePath
* @return
* @throws IOException
*/
public static Map<String,Object> getAllKeysAndValues(String filePath) throws IOException{
Properties properties=getProperties(filePath);
//When initializing a map, it is best to specify the initial value size
Map<String,Object> map=new HashMap<String, Object>(2);
List<String> keys=getAllKeys(filePath);
List<String> values=new ArrayList<String>();
if(keys!=null){
for(int i=0;i<keys.size();i++){
String value=properties.getProperty(keys.get(i));
values.add(value);
}
}
map.put("keys", keys);
map.put("values", values);
return map;
}
/**
* Get the value value corresponding to the key specified in the properties configuration file
* @param filePath The path of the file relative to the root
* @param propertyName key
* @return value
* @throws IOException
*/
public static String getPropertyValue(String filePath,String propertyName) throws IOException{
Properties props=getProperties(filePath);
return props.getProperty(propertyName);
}
/**
* Update the key value of the configuration file
* @param filePath The path of the file relative to the root
* @param propertyName key
* @param value value
* @param flag true Or false, false for overwrite, true for append
* @throws IOException
*/
public static void setPropertyValue(String filePath,String propertyName,String value,boolean flag) throws IOException{
Properties props=getProperties(filePath);
props.setProperty(propertyName, value);
String outPath=getRealPath(filePath);
//true for append, false for overwrite
FileOutputStream out=new FileOutputStream(outPath,flag);
//The second parameter of the store represents the comment, which will be written to the properties file
props.store(out, "\nupdate "+propertyName);
out.close();
out.flush();
}
/**
* Batch update key value of configuration file
* @param filePath The path of the file relative to the root
* @param propNames key array
* @param values value array
* @param flag true Or false, false for overwrite, true for append
* @throws IOException
*/
public static void setProperties(String filePath,String[] propNames,String[] values,boolean flag) throws IOException{
if(propNames==null || values==null || propNames.length==0 || values.length==0 || propNames.length!=values.length){
return;
}
Properties properties=getProperties(filePath);
for(int i=0;i<values.length;i++){
if(StringUtils.isBlank(propNames[i])){
continue;
}
properties.setProperty(propNames[i], values[i]);
}
String outPath=getRealPath(filePath);
//flag: true for append, false for overwrite
FileOutputStream out=new FileOutputStream(outPath,flag);
properties.store(out,"Batch update key: "+Arrays.toString(propNames));
out.close();
out.flush();
}
/**
* Remove key
* @param filePath The path of the file relative to the root
* @param propertyName key
* @throws IOException
*/
public static void removeProperty(String filePath,String propertyName) throws IOException{
Properties props=getProperties(filePath);
props.remove(propertyName);
String outPath=getRealPath(filePath);
FileOutputStream out=new FileOutputStream(outPath,false);
props.store(out, "delete "+propertyName);
out.close();
out.flush();
}
/**
* Batch remove key
* @param filePath The path of the file relative to the root
* @param keys key aggregate
* @throws IOException
*/
public static void removeProperties(String filePath,String[] keys) throws IOException{
Properties props=getProperties(filePath);
if(keys!=null){
for(int i=0;i<keys.length;i++){
if(StringUtils.isNotBlank(keys[i])){
props.remove(keys[i]);
}
}
}
String outPath=getRealPath(filePath);
FileOutputStream out=new FileOutputStream(outPath,false);
props.store(out, "delete "+Arrays.toString(keys));
out.close();
out.flush();
}
}
Note: note above:
//flag: true for append, false for overwrite
FileOutputStream out=new FileOutputStream(outPath,flag);
If the flag is true, the format of the properties file after the operation is as follows:
That is to say, every operation record will be kept. If it is false, it will not.
**
Note: the properties after the operation are located in the target/classes directory, and the test.properties file in the src directory will not be changed.
**