读写ini 和 properties文件


***********************************.properties文件****************************

public static void main(String[] args) throws Exception {
Properties prop = new Properties();//属性集合对象
FileInputStream fis = new FileInputStream("engineToolTestSrc/config/config.properties");//属性文件输入流
prop.load(fis);//将属性文件流装载到Properties对象中
fis.close();//关闭流

//获取属性值,sitename已在文件中定义
System.out.println("获取属性值:name=" + prop.getProperty("name"));
//获取属性值,country未在文件中定义,将在此程序中返回一个默认值,但并不修改属性文件
System.out.println("获取属性值:password=" + prop.getProperty("password", "中国"));
System.out.println("获取" + prop.keySet().size());
System.out.println("获取" + prop.values().size());
System.out.println("获取" + prop.stringPropertyNames().size());
Enumeration enums = prop.keys();
while (enums.hasMoreElements()) {
Object key = enums.nextElement();
System.out.println("key = " + key + " value = " + prop.get(key));
}

// //修改sitename的属性值
// prop.setProperty("name", "Boxcode");
// //添加一个新的属性studio
// prop.setProperty("password", "Boxcode Studio");
// //文件输出流
// FileOutputStream fos = new FileOutputStream("engineToolTestSrc/config/config.properties");
// //将Properties集合保存到流中
// prop.store(fos, "Copyright (c) Boxcode Studio");
// fos.close();//关闭流
}

######################################.nin文件###########################

public class FileUtil {

  /**

  * 读取INI配置

  * @param file INI配置文件完整路径

  * @param sec 项

  * @param key 键

  * @param defaults 默认值

  * @return

  */

  @SuppressWarnings("unchecked")

  public static String GetPrivateProfileString(String file,String sec,String key,String defaults)

  {

  String result = defaults;

  Map map = getIniAllValue(file);

  if(map==null)

  return result;

  ArrayList section = (ArrayList)map.get(sec);

  if(section!=null)

  {

  Iterator iter = section.iterator();

  while(iter.hasNext()){

  String[] kv = (String[])iter.next();

  if(kv!=null && kv[0].equals(key.trim())){

  return dealCorpsSign(kv[1],2);

  }

  }

  }

  return defaults;

  }

  /**

  * 写入配置 INI文件

  * @param file INI配置文件完整路径

  * @param sec 项

  * @param key 键

  * @param value 值

  * @return

  */

  @SuppressWarnings("unchecked")

  public static boolean WritePrivateProfileString(String file,String sec,String key,String value)

  {

  value = dealCorpsSign(value,1);

  Map map = getIniAllValue(file);

  if(map==null)

  {

  map = new HashMap();

  ArrayList section = new ArrayList();

  section.add(new String[]{key,value});

  map.put(sec, section);

  }

  else {

  int x = 0,y = 0;

  ArrayList al = (ArrayList)map.get(sec);

  if(al!=null){

  Iterator iter = al.iterator();

  while(iter.hasNext()){

  x++;

  String[] kv = (String[])iter.next();

  if(kv!=null && kv[0].equals(key)){

  kv[1] = value;

  y++;

  }

  }

  }

  if(x==0){

  ArrayList section = new ArrayList();

  section.add(new String[]{key,value});

  map.put(sec, section);

  }

  else if(y==0){

  al.add(new String[]{key,value});

  map.put(sec, al);

  }

  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值