properties配置文件读写,追加

在开发中常用properties文件来存储系统配置信息,下面就properties文件的读写,信息追加作简要介绍,顺便也解决乱码问题。 
1、首先介绍一下properties类 
properties类继承自Hashtable

package com.gmi.client.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Reader; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import java.util.Properties;  public class PropUtil { public static String readProperty(String file, String key) { InputStream is = null; FileInputStream fis = null; Properties prop = null; try { prop = new Properties(); fis = new FileInputStream(file); is = new BufferedInputStream(fis); prop.load(is); String value = new String(prop.getProperty(key, "").getBytes("ISO-8859-1"), "UTF-8"); return value; } catch (Exception e) { e.printStackTrace(); return ""; } finally { if (prop != null) prop = null; //fis try { if (fis != null) { fis.close(); fis = null; } } catch (Exception e) { e.printStackTrace(); } //is try { if (is != null) { is.close(); is = null; } } catch (Exception e) { e.printStackTrace(); } } } public static String readLocalProperty(String key) { String path = ""; try { path = (PropUtil.class.getClassLoader().getResource("").toURI()).getPath(); } catch (URISyntaxException e) { e.printStackTrace(); } return readProperty(path + "conf.properties", key); } public static void writeProperty(String filename,String key,String value){ FileInputStream fis = null; Properties properties = new Properties(); try { fis = new FileInputStream(filename); BufferedReader bf = new BufferedReader(new InputStreamReader(fis,"UTF-8")); properties.load(bf); OutputStream out = new FileOutputStream(filename); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out,"UTF-8")); properties.setProperty(key, value); properties.store(bw, "propertylist"); out.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void writeLocalProperty(String key,String value){ String path = ""; try { path = (PropUtil.class.getClassLoader().getResource("").toURI()).getPath(); } catch (URISyntaxException e) { e.printStackTrace(); } writeProperty(path+"conf.properties", key, value); } public static List<Integer> parseNum2List(String numStr) { List<Integer> list = new ArrayList<Integer>(); StringBuilder sb = null; int len = numStr.length(); for (int i = 0;i < len;i++) { char ch = numStr.charAt(i); if (Character.isDigit(ch)) { if(null == sb){ sb = new StringBuilder(); } sb.append(ch); } else { if (null != sb) { list.add(Integer.parseInt(sb.toString())); sb = null; } } } // Add the last num if (null != sb) { list.add(Integer.parseInt(sb.toString())); } return list; } }

获取部署项目的properties文件路径:PropUtil.class.getClassLoader().getResource(“”).toURI()).getPath(); 
配置信息追加:首先把properties文件中原有的信息load出来,然后在store,这样就不存在清空原信息的问题
fis = new FileInputStream(filename); 
BufferedReader bf = new BufferedReader(new InputStreamReader(fis,”UTF-8”)); 
properties.load(bf); 

转载于:https://www.cnblogs.com/jsjjob/p/8196423.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值