`
decao
  • 浏览: 78029 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

修改properties文件

阅读更多
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       :  ModifyProperties
 * @author     :  ncc
 * create time :  2017-12-5 上午11:59:00
 * @version    :  1.0  
 * description :  修改properties文件
 * @see        :                        
 * ************************************************/   
public class ModifyProperties {
	/* ********************************************
	 * method name   : writeData 
	 * description   : 修改properties文件,将key对应的键值改为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) {
		// 获取绝对路径
		filePath = ModifyProperties.class.getResource("/" + filePath).toString();
		System.out.println(filePath);
		// 截掉路径的”file:/“前缀
		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);
			// 一定要在修改值之前关闭fis
			fis.close();
			OutputStream fos = new FileOutputStream(filePath);
			prop.setProperty(key, value);
			// 保存,并加入注释
			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");
	}
}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics