package com.bohui.ipview.common; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; public class PropertiesUtil { //参数为要修改的文件路径 以及要修改的属性名和属性值 public static Boolean updatePro(String path,String key,String value){ Properties prop = new Properties();// 属性集合对象 FileInputStream fis; try { fis = new FileInputStream(path); prop.load(fis);// 将属性文件流装载到Properties对象中 fis.close();// 关闭流 } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } prop.setProperty(key, value); // 文件输出流 try { FileOutputStream fos = new FileOutputStream(path); // 将Properties集合保存到流中 prop.store(fos, "Copyright (c) Boxcode Studio"); fos.close();// 关闭流 } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } System.out.println("获取修改后的属性值:password=" + prop.getProperty("password")); return true; } }
Java修改properties文件,布布扣,bubuko.com
原文:http://blog.csdn.net/caohaicheng/article/details/21031169