wdzwdz
Posts: 3
Nickname: wdzwdz
Registered: Dec, 2002
|
|
Re: how to use the '.ini' file in java program?
|
Posted: Dec 4, 2002 5:44 PM
|
|
package wdz200211151;
/** * <p>Title: </p> * <p>Description:Properties?????????????????????????????? </p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company:netsky </p> * <p>Date: 2002-11-15</p> * @author wdz * @version 1.0 */ import java.util.*; import java.io.*; public class Untitled1 {
/** ???????????????? * Properties ??Hashtable???????????????????????????????????? * public class Properties extends Hashtable * * @param Properties ???????????????? * @param fileName ???????? * */ static public void saveProperties(Properties p,String fileName){ java.io.FileOutputStream out=null; try { out =new FileOutputStream(fileName) ; } catch(Exception e){ System.out.println("error create file error!"+e.getMessage()); }
try{ p.store(out,"saveproperty"); } catch(Exception e){ System.out.println("error save file error!"+e.getMessage()); } }
/** ???????????????? * Properties ??Hashtable???????????????????????????????????? * public class Properties extends Hashtable * * @param fileName ???????? * @return ?????????????????? * */ static public Properties loadProperties(String fileName){ Properties p=new Properties(); FileInputStream in=null ; try{ in = new FileInputStream(fileName);} catch(Exception e){ System.out.println("error create file error!"+e.getMessage()); }
try { p.load(in); } catch(Exception e){ System.out.println("error load file error!"+e.getMessage()); } return p; }
/**???????????????????????????????????? * Properties ??Hashtable???????????????????????????????????? * public class Properties extends Hashtable * * @param Properties Properties???? * @sKey ???????? * @return ?????????????? * */ static public String getAPropertyValue(Properties p,String sKey){ String sValue="";
if (p.containsKey(sKey)){ sValue = p.getProperty(sKey); }
return sValue;
}
/**???????????????????????????????? * Properties ??Hashtable???????????????????????????????????? * public class Properties extends Hashtable * * @param fileName ?????????????? * @param sKey ???????? * @return ?????????????? * */ static public String getAPropertyValue(String fileName,String sKey){ String value=""; Properties wdzp = loadProperties( fileName); value = getAPropertyValue(wdzp,sKey); return value; }
public static void main(String[] args) { Untitled1 untitled11 = new Untitled1(); String fileName="wdzwdz.txt";
Properties p = new Properties();
p.put("author","wdz"); p.put("date","2002-11-15"); p.put("corp","netsky"); p.put("description","Properties??????! ");
//test save Properties saveProperties(p,fileName);
//test load Properties Properties pp =loadProperties(fileName); System.out.println(pp);
//test load Property from Properties list String author = getAPropertyValue(pp,"author"); System.out.println("author="+author);
//test load Property from a file String corp = getAPropertyValue(fileName,"corp"); System.out.println("corp="+corp); } }
|
|