java修改读取properties配置文件中的内容

java修改properties配置文件中的内容

	/**
	 * 修改properties配置文件中data_time的值
	 * @param bigtime 开始时间
	 * @param endtime 结束时间
	 * @throws IOException
	 */
	public static void getProperties(String bigtime,String endtime) throws IOException {
		String path = Thread.currentThread().getContextClassLoader()
				.getResource("").getPath().substring(1);
		FileInputStream fis = new FileInputStream(path + "classes/email.properties");// 属性文件输入流
		byte[] buf = new byte[fis.available()];
		StringBuffer sb = new StringBuffer();
		while ((fis.read(buf)) != -1) {
			sb.append(new String(buf));
			buf = new byte[1024];// 重新生成,避免和上次读取的数据重复
		}
		//得到修改后的字符串
		StringBuffer pr = sb.replace(sb.indexOf("data_time")+10, sb.indexOf("#数据"), bigtime+"-"+endtime+"\r\n");
		fis.close();// 关闭流

		// 文件输出流
		FileOutputStream fos = new FileOutputStream(path + "classes/email.properties");
		// 将Properties集合保存到流中
		fos.write(pr.toString().getBytes());
		fos.close();// 关闭流
	}


读取properties配置文件中的内容

public class PropertiesUtils {

	private static final String LOCATION = "oao.properties";

	private static Properties properties = new Properties();

	/**
	 * 根据KEY获取参数
	 * 
	 * @param key
	 * @return
	 */
	public static String getProperties(String key) {

		String result = null;
		InputStream input = null;
		try {
			input = PropertiesUtils.class.getClassLoader().getResourceAsStream(LOCATION);
			properties.load(input);

			result = properties.getProperty(key);
		} catch (IOException e) {
			e.printStackTrace();
			result = "";
		} finally {
			if (input != null) {
				try {
					input.close();
				} catch (IOException e) {
					e.printStackTrace();
					result = "";
				}
			}
		}

		return result;
	}

	public static void main(String[] args) {
		System.out.println(getProperties("ACCOUNT_PAGE_SIZE"));
	}

}


oao.properties内容

ACCOUNT_PAGE_SIZE=20




  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yn00

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值