package cn.com.mfsoft.config; import java.io.*; import java.net.URL; import java.net.URLDecoder; import java.util.*; import org.apache.log4j.Logger; import cn.com.mfsoft.config.ConfigurationException; public class Configuration { private Properties config=new Properties();//记录配置项 private String fn=null;//记录配置文件名 //此构造方法用于新建配置文件 public Configuration() { String path=""; try { //File file = new File("system.conf"); URL u = this.getClass().getResource(this.getClass().getSimpleName()+".conf"); path = u.getPath(); //获得用户工程目录System.getProperty("user.dir") //path=(System.getProperty("user.dir").replace("\\", "/"))+"/model/Model_"+model_id+".snet"; // path=URLDecoder.decode(path, "utf-8"); System.out.println("11"+path); path=URLDecoder.decode(path, "utf-8"); System.out.println("22"+path); FileInputStream fin = new FileInputStream(path); config.load(fin); //载入文件 fin.close(); } catch (IOException ex) { try { throw new ConfigurationException ("无法读取指定的配置文件:"+path); } catch (ConfigurationException e) { e.printStackTrace(); } } fn=path; } public String getPrjRoot() { URL u = this.getClass().getResource(this.getClass().getSimpleName()+".conf"); String str = u.getPath(); if (str.indexOf(":") != -1) str = str.substring(1);// 在windows下面为/F:/MyPrj/WORK/post else str = str.substring(0);// 在Linux下面为/usr/local/apache... return str; } //从指定文件名读入配置信息 public Configuration(String fileName)throws ConfigurationException { try { FileInputStream fin = new FileInputStream(fileName); config.load(fin); //载入文件 fin.close(); } catch (IOException ex) { throw new ConfigurationException ("无法读取指定的配置文件:"+fileName); } fn=fileName; } //指定配置项名称,返回配置值 public String getValue(String itemName) { String value=getValue("language","chinese"); String st=""; if(value.equals("chinese")) { st=toGb(config.getProperty(itemName)); } else { st=config.getProperty(itemName); } return st;//config.getProperty(itemName); } public String getValue2(String ItemName) { String value=""; try { String path=""; path=getClass().getResource("/system.conf").getPath().substring(1); path=URLDecoder.decode(path, "utf-8"); System.out.println(); Configuration config= new Configuration(path); value=config.getValue(ItemName); } catch (Exception e) { e.printStackTrace(); } return value; } //指定配置项名称和默认值,返回配置值 public String getValue(String itemName, String defaultValue) { return config.getProperty(itemName,defaultValue); } //设置配置项名称及其值 public void setValue(String itemName,String value){ config.setProperty(itemName,value); return; } //保存配置文件,指定文件名和抬头描述 public void saveFile(String fileName,String description)throws ConfigurationException { try { FileOutputStream fout = new FileOutputStream(fileName); config.store(fout, description);//保存文件 fout.close(); } catch (IOException ex) { throw new ConfigurationException ("无法保存指定的配置文件:"+fileName); } } //保存配置文件,指定文件名 public void saveFile(String fileName)throws ConfigurationException { saveFile(fileName,""); } //保存配置文件,采用原文件名 public void saveFile() throws ConfigurationException { if(fn.length()==0) throw new ConfigurationException ("需指定保存的配置文件名"); saveFile(fn); } public static String toGb(String uniStr) { String gbStr = ""; if(uniStr == null) { uniStr = ""; } try { byte[] tempByte = uniStr.getBytes("ISO8859_1"); gbStr = new String(tempByte,"GB2312"); } catch(Exception ex) { } return gbStr; } public static void main(String[]args) { } } ConfigurationException.java /** * *<p>Blog:http://www.cnweblog.com/nm1504</p> *<p>E-mail:yyk1504@163.com</p> *<p>创建时间:2008-3-13-下午03:12:29</p> *<p>Copyright: (c)2008-3-13</p> */ package cn.com.mfsoft.config; public class ConfigurationException extends Exception { public ConfigurationException() { } public ConfigurationException(String msg) { super(msg); } } ReadConfig.java /** **<p>Blog:http://www.cnweblog.com/nm1504</p> *<p>E-mail:yyk1504@163.com</p> *<p>创建时间:2008-3-13-下午03:14:24</p> *<p>Copyright: (c)2008-3-13</p> */ package cn.com.mfsoft.config; import java.net.URL; public class ReadConfig { public static void main(String[] args) { try { Configuration config=new Configuration(); //读取指定文件 ReadConfig rc=new ReadConfig(); System.out.println("::::::;"+rc.getPrjRoot()); try{ /* System.out.println(new String(config.getValue("Max_Users_Count")));//5 System.out.println(new String(config.getValue("Max_Users_Count").getBytes(),"GB2312"));//6 System.out.println(new String(config.getValue("Max_Users_Count").getBytes(),"ISO8859_1"));//7 System.out.println(new String(config.getValue("Max_Users_Count").getBytes("GB2312")));//8 System.out.println(new String(config.getValue("Max_Users_Count").getBytes("GB2312"),"GB2312"));//9 System.out.println(new String(config.getValue("Max_Users_Count").getBytes("GB2312"),"ISO8859_1"));//10 System.out.println(new String(config.getValue("Max_Users_Count").getBytes("UTF-8")));//11 System.out.println(new String(config.getValue("Max_Users_Count").getBytes("UTF-8"),"GB2312"));//12 System.out.println(new String(config.getValue("Max_Users_Count").getBytes("ISO8859_1"),"UTF-8"));//13 System.out.println(toGb(config.getValue("Max_Users_Count")));*/ } catch(Exception e) { e.printStackTrace(); } } catch(Exception ex) { ex.printStackTrace(); } } public String getPrjRoot() { URL u = this.getClass().getResource(this.getClass().getSimpleName()+".conf"); String str = u.getPath(); if (str.indexOf(":") != -1) str = str.substring(1);// 在windows下面为/F:/MyPrj/WORK/post else str = str.substring(0);// 在Linux下面为/usr/local/apache... return str; } } SetConfig.java /** * *<p>Blog:http://www.cnweblog.com/nm1504</p> *<p>E-mail:yyk1504@163.com</p> *<p>创建时间:2008-3-13-下午03:13:20</p> *<p>Copyright: (c)2008-3-13</p> */ package cn.com.mfsoft.config; import cn.com.mfsoft.config.ConfigurationException;//包含这个包方能使用配置类 public class SetConfig { public static void main(String[] args) { try { Configuration config = new Configuration(); //设置一些属性值 config.setValue("language", "chinese"); config.setValue("Max_Users_Count", "50"); config.setValue("Max_OpenedFile_Count", "38"); //保存文件 config.saveFile("d:/system.conf", "Sytem Global Configuration"); } catch (ConfigurationException ex) { //捕获我们自定义的异常 ex.printStackTrace(); } } }