java配置文件读写类--从网上看到的

package com.weemambo.common.Config;

import java.util.*;
import java.io.*;

public class ConfigFile {
 /**   配置文件名   */
 private static final String strFileName = "dayEnd.properties";

 public ConfigFile() {
 }

 /**
  * 读取配置项
  * @param key-属性名称
  * @return 属性值
  */
 public static String findProperty(String key) {
  File CfgFile = new File(getPath() + "//" + strFileName);
  try {
   if (!CfgFile.exists()) {
    CfgFile.createNewFile();//没有则创建该文件,不过,该文件内容是空的
   }
   Properties prop = new Properties();
   prop.load(new FileInputStream(CfgFile));
   return ISO2GB(prop.getProperty(key));//一般的配置文件是iso2的编码格式,将他转换为gb2312
  } catch (IOException ex) {
   ex.printStackTrace();
   return null;
  }
 }

 /**
  * 设置配置文件
  * @param key-属性名称
  * @param value-属性值
  */
 public static void saveProperty(String key, String value) {
  File CfgFile = new File(getPath() + "//" + strFileName);
  value = replaceBacklashToDouble(value);
  try {
   Properties prop = new Properties();
   
   //首先对配置文件的每一项进行编码格式转换
   if (CfgFile.exists()) {
    prop.load(new FileInputStream(CfgFile));
    Iterator iter = prop.keySet().iterator();
    for (; iter.hasNext();) {
     String key1 = (String) iter.next();
     prop.setProperty(key1, ISO2GB(prop.getProperty(key1)));
    }
   }

   //然后再改变属性值
   prop.setProperty(key, value);

   //文件不存在,则创建文件
   if (!CfgFile.exists()) {
    CfgFile.createNewFile();
   }

   //调用相关类,想文件中写入键值对
   PrintWriter writer = new PrintWriter(new FileOutputStream(CfgFile));
   Iterator iter = prop.entrySet().iterator();
   for (; iter.hasNext();) {
    Map.Entry entry = (Map.Entry) iter.next();
    writer.println(entry.getKey() + "="
      + ((String) entry.getValue()));
   }
   writer.close();
  } catch (IOException ex) {
   ex.printStackTrace();
  }
 }

 /**  
  *   将ISO-8859-1字符编码转为GB2312字符编码  
  *   @param   source   要进行转码的字符串  
  *   @return   转码后的字符串  
  */
 private static String ISO2GB(String source) {
  if (source == null || source.length() == 0) {
   return "";
  }

  String target = source;
  try {
   target = new String(source.getBytes("8859_1"), "GB2312");
  } catch (Exception e) {
   e.printStackTrace();
   System.out.println("由ISO-8859-1到GB2312转码失败!");
  }
  return target;
 }

 /**  
  *   获取当前项目所在的目录  
  *   @return   当前项目所在的目录  
  */
 public static String getPath() {
  String strResult = System.getProperty("user.dir");
  return strResult;
 }

 /**  
  *   将字符串中的反斜杠("/")替换成双反斜杠("//")  
  *   @param   pStr   需要替换的字符串  
  *   @return   替换后的字符串  
  */
 public static String replaceBacklashToDouble(String pStr) {
  if (pStr.length() < 1) {
   return "";
  }
  StringTokenizer stk = new StringTokenizer(pStr, "//", true);
  Vector v = new Vector();
  while (stk.hasMoreTokens()) {
   v.addElement(stk.nextToken());
  }

  String strResult = v.get(0).toString();
  for (int i = 1; i < v.size(); i++) {
   if (v.get(i).toString().trim().equals("//")) {
    strResult += "//" + v.get(i).toString();
   } else {
    strResult += v.get(i).toString();
   }
  }
  return strResult;
 }

 public static void main(String[] args) {
  System.out.println("读取配置项file_namer   ===   "
    + ConfigFile.findProperty("ins_c_schedule_hour"));
//  System.out
//    .println(replaceBacklashToDouble(getPath()));
//  System.out.println("读取配置项Path   ===   "
//    + ConfigFile.findProperty("Path"));
//  System.out
//    .println("==========================================================");
//  ConfigFile.saveProperty("User", "sa");
//  System.out.println("设置配置项User   ===   sa");
//  ConfigFile.saveProperty("Pwd", "123456");
//  System.out.println("设置配置项Pwd   ===   123456");
//  ConfigFile.saveProperty("Path", "D://cx//config");
//  System.out.println("设置配置项Path   ===   D://cx//config");
//  System.out
//    .println("==========================================================");
//  System.out.println("读取配置项User   ===   "
//    + ConfigFile.findProperty("User"));
//  System.out
//    .println("读取配置项Pwd   ===   " + ConfigFile.findProperty("Pwd"));
//  System.out.println("读取配置项Path   ===   "
//    + ConfigFile.findProperty("Path"));
 }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值