Posted on 2007-10-05 23:15 
kooyee 阅读(781) 
评论(1)  编辑  收藏  所属分类: 
Java  
			 
			
		 
		由于经常从文本读写属性设置到代码中,所以利用java的properties类带取代对文件的操作。
首先建立properties文本文件并输入
Host_name = database name...
username = 用户名
password = 密码
读取properties文件的代码
 Properties props =  new Properties();
Properties props =  new Properties();
 props.load(new FileInputStream("./database.properties"));
        props.load(new FileInputStream("./database.properties"));
 hostname = props.getProperty("Host_name");
        hostname = props.getProperty("Host_name");
 username = props.getProperty("User_name");
        username = props.getProperty("User_name");
 password = props.getProperty("Password");
        password = props.getProperty("Password");
这样在code外部(在properties文本文件中)修改属性后, 通过properties导入到代码中对应的值里。
java properties 类
 private FileOutputStream out;
private FileOutputStream out;


 /** *//**
  /** *//**

 * 根据传进的文件名载入文件
   * 根据传进的文件名载入文件

 * @param fileName String
   * @param fileName String

 */
   */


 public PropertiesUtil(String fileName)
  public PropertiesUtil(String fileName)  {
{

 this.fileName=fileName;
    this.fileName=fileName;

 File file = new File(fileName);
    File file = new File(fileName);


 try
    try  {
{

 in = new FileInputStream(file);
      in = new FileInputStream(file);

 p = new Properties();
      p = new Properties();

 //载入文件
      //载入文件

 p.load(in);
      p.load(in);

 in.close();
      in.close();

 }
    }


 catch (FileNotFoundException e)
    catch (FileNotFoundException e)  {
{

 System.err.println("配置文件config.properties找不到!!");
      System.err.println("配置文件config.properties找不到!!");

 e.printStackTrace();
      e.printStackTrace();

 }
    }


 catch (Exception e)
    catch (Exception e)  {
{

 System.err.println("读取配置文件config.properties错误!!");
      System.err.println("读取配置文件config.properties错误!!");

 e.printStackTrace();
      e.printStackTrace();

 }
    }

 }
  }

 
 


 /** *//**
  /** *//**

 * 配置文件一律为config.propertities,并且统一放在web应用的根目录下。
   * 配置文件一律为config.propertities,并且统一放在web应用的根目录下。

 * @return String
   * @return String

 */
   */


 public static String getConfigFile(HttpServlet hs)
  public static String getConfigFile(HttpServlet hs)  {
{

 
 

 return getConfigFile(hs,"config.properties");
    return getConfigFile(hs,"config.properties");

 }
  }


 /** *//**
  /** *//**

 * 在servlet中使用,直接用this作为参数,HttpServlet类型
   * 在servlet中使用,直接用this作为参数,HttpServlet类型

 * 根据配置文件名从当前web应用的根目录下找出配置文件
   * 根据配置文件名从当前web应用的根目录下找出配置文件

 * @param hs HttpServlet
   * @param hs HttpServlet

 * @param configFileName String配置文件名字
   * @param configFileName String配置文件名字

 * @return String
   * @return String

 */
   */


 public static String getConfigFile(HttpServlet hs, String configFileName)
  public static String getConfigFile(HttpServlet hs, String configFileName)  {
{

 String configFile = "";
    String configFile = "";

 ServletContext sc = hs.getServletContext();
    ServletContext sc = hs.getServletContext();

 configFile = sc.getRealPath("/" + configFileName);
    configFile = sc.getRealPath("/" + configFileName);


 if (configFile == null || configFile.equals(""))
    if (configFile == null || configFile.equals(""))  {
{

 configFile = "/" + configFileName;
      configFile = "/" + configFileName;

 }
    }

 return configFile;
    return configFile;

 }
  }


 /** *//**
  /** *//**

 * jsp中用pageContext作参数
   * jsp中用pageContext作参数

 * @param hs PageContext
   * @param hs PageContext

 * @param configFileName String 配置文件名字
   * @param configFileName String 配置文件名字

 * @return String
   * @return String

 */
   */


 public static String getConfigFile(PageContext hs, String configFileName)
  public static String getConfigFile(PageContext hs, String configFileName)  {
{

 String configFile = "";
    String configFile = "";

 ServletContext sc = hs.getServletContext();
    ServletContext sc = hs.getServletContext();

 configFile = sc.getRealPath("/" + configFileName);
    configFile = sc.getRealPath("/" + configFileName);


 if (configFile == null || configFile.equals(""))
    if (configFile == null || configFile.equals(""))  {
{

 configFile = "/" + configFileName;
      configFile = "/" + configFileName;

 }
    }

 return configFile;
    return configFile;

 }
  }

 
 


 /** *//**
  /** *//**

 * 列出所有的配置文件内容
   * 列出所有的配置文件内容

 */
   */


 public void list()
  public void list()  {
{

 p.list(System.out);
    p.list(System.out);

 }
  }

 
 


 /** *//**
  /** *//**

 * 指定配置项名称,返回配置值
   * 指定配置项名称,返回配置值

 * @param itemName String
   * @param itemName String

 * @return String
   * @return String

 */
   */


 public String getValue(String itemName)
  public String getValue(String itemName) {
{

 return p.getProperty(itemName);
    return p.getProperty(itemName);

 }
  }

 
 


 /** *//**
  /** *//**

 * 指定配置项名称和默认值,返回配置值
   * 指定配置项名称和默认值,返回配置值

 * @param itemName String
   * @param itemName String

 * @param defaultValue String
   * @param defaultValue String

 * @return String
   * @return String

 */
   */

 public String getValue(String itemName,
  public String getValue(String itemName,


 String defaultValue)
                         String defaultValue) {
{

 return p.getProperty(itemName,defaultValue);
    return p.getProperty(itemName,defaultValue);

 }
  }

 
 


 /** *//**
  /** *//**

 * 设置配置项名称及其值
   * 设置配置项名称及其值

 * @param itemName String
   * @param itemName String

 * @param value String
   * @param value String

 */
   */


 public void setValue(String itemName,String value)
  public void setValue(String itemName,String value) {
{

 p.setProperty(itemName,value);
    p.setProperty(itemName,value);

 return;
    return;

 }
  }

 
 


 /** *//**
  /** *//**

 * 保存配置文件,指定文件名和抬头描述
   * 保存配置文件,指定文件名和抬头描述

 * @param fileName String
   * @param fileName String

 * @param description String
   * @param description String

 * @throws Exception
   * @throws Exception

 */
   */


 public void saveFile(String fileName,String description)throws Exception
  public void saveFile(String fileName,String description)throws Exception {
{


 try
    try  {
{

 File f=new File(fileName);
      File f=new File(fileName);

 out
      out

 = new FileOutputStream(f);
          = new FileOutputStream(f);

 p.store(out, description);//保存文件
      p.store(out, description);//保存文件

 out.close();
      out.close();

 }
    }


 catch (IOException ex)
    catch (IOException ex)  {
{

 throw new Exception
      throw new Exception

 ("无法保存指定的配置文件:"+fileName);
          ("无法保存指定的配置文件:"+fileName);

 }
    }

 }
  }

 
 


 /** *//**
  /** *//**

 * 保存配置文件,指定文件名
   * 保存配置文件,指定文件名

 * @param fileName String
   * @param fileName String

 * @throws Exception
   * @throws Exception

 */
   */

 public void saveFile(String fileName)
  public void saveFile(String fileName)


 throws Exception
      throws Exception  {
{

 saveFile(fileName,"");
    saveFile(fileName,"");

 }
  }

 
 


 /** *//**
  /** *//**

 * 保存配置文件,采用原文件名
   * 保存配置文件,采用原文件名

 * @throws Exception
   * @throws Exception

 */
   */


 public void saveFile() throws Exception
  public void saveFile() throws Exception  {
{

 if(fileName.length()==0)
    if(fileName.length()==0)

 throw new Exception
      throw new Exception

 ("需指定保存的配置文件名");
          ("需指定保存的配置文件名");

 saveFile(fileName);
    saveFile(fileName);

 }
  }


 /** *//**
  /** *//**

 * 删除一个属性
   * 删除一个属性

 * @param value String
   * @param value String

 */
   */


 public void deleteValue(String value)
  public void deleteValue(String value) {
{

 p.remove(value);
    p.remove(value);

 }
  }


 /** *//**
  /** *//**

 * main method for test
   * main method for test

 * @param args String[]
   * @param args String[]

 */
   */


 public static void main(String[] args)
  public static void main(String[] args)  {
{

 String file = "f:\\p.properties";
    String file = "f:\\p.properties";

 PropertiesUtil pu = new PropertiesUtil(file);
    PropertiesUtil pu = new PropertiesUtil(file);

 pu.list();
    pu.list();

 }
  }

 }
}
