Modify the properties file

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;


/* ******************** Class description ********************
 * class       :  ModifyProperties
 * @author     :  ncc
 * create time : 2017-12-5 11:59:00 am
 * @version    :  1.0  
 * description : Modify the properties file
 * @see        :                        
 * ************************************************/   
public class ModifyProperties {
	/* ********************************************
	 * method name   : writeData
	 * description : Modify the properties file and change the key value corresponding to the key to value
	 * @return       : void
	 * @param        : @param filePath
	 * @param : @param key
	 * @param        : @param value
	 * modified      : ncc ,  2017-12-5
	 * @see          :
	 * ********************************************/      
	public static void writeData(String filePath, String key, String value) {
		// get absolute path
		filePath = ModifyProperties.class.getResource("/" + filePath).toString();
		System.out.println(filePath);
		// Truncate the "file:/" prefix of the path
		filePath = filePath.substring(6);
		System.out.println(filePath);
		Properties prop = new Properties();
		try {
			File file = new File(filePath);
			if(!file.exists())
				file.createNewFile();
			InputStream fis = new FileInputStream(file);
			prop.load(fis);
			// be sure to close fis before modifying the value
			fis.close();
			OutputStream fos = new FileOutputStream(filePath);
			prop.setProperty(key, value);
			// save and add comments
			prop.store(fos, "Update '" + key + "' value");
			fos.close();
		} catch (IOException e) {
			System.err.println("Visit " + filePath + " for updating " + value + " value error");
		}
	}
	/* ********************************************
	 * method name   : main
	 * description   :
	 * @return       : void
	 * @param        : @param args
	 * modified      : ncc ,  2017-12-5
	 * @see          :
	 * ********************************************/      
	public static void main(String[] args) {
		writeData("config\\mapping_merinfo.properties","signKey","564789321");
	}
}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326485735&siteId=291194637