日期:2014-05-18  浏览次数:20783 次

properties 使用问题
package zwf.xml;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;


public class Configuration{
 
  private Properties propertie;
  private String path;
  private FileInputStream inputFile;
  private FileOutputStream outputFile;
  public Configuration(){
  propertie = new Properties();
  }
 public Configuration(String filePath){
  path = filePath;
  propertie = new Properties();
  try {
  inputFile = new FileInputStream(filePath);
  propertie.load(inputFile);
  inputFile.close();
  } catch (FileNotFoundException ex){
  System.out.println("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");
  ex.printStackTrace();
  } catch (IOException ex){
  System.out.println("装载文件--->失败!");
  ex.printStackTrace();
  }
  }
  
  
  public String getValue(String password){
  if(propertie.containsKey(password)){
  String value = propertie.getProperty(password);//得到某一属性的值
  return value;
  }
  else
  return "";
  }
  
  
  public void clear(){
  propertie.clear();
  }
  
  
  public void setValue(String password, String value) throws IOException{
  propertie.setProperty(password, value);
  FileOutputStream fos = new FileOutputStream("D:/myjweb/workspace/cxxue/src/zwf/xml/test.properties"); 
  propertie.store(fos, "Copyright (c) Boxcode Studio"); 
  fos.close();

  }
   
   
  public void saveFile(String description){
  try {
  outputFile = new FileOutputStream(path);
  propertie.store(outputFile, description);
  outputFile.close();
  } catch (FileNotFoundException e) {
  e.printStackTrace();
  } catch (IOException ioe){
  ioe.printStackTrace();
  }
  }
  
  
   
   
  public static void main(String[] args) {
  Configuration rc = new Configuration("D:/myjweb/workspace/cxxue/src/zwf/xml/test.properties");
   
  //测试从文件读取
   
  String password = rc.getValue("password");
  System.out.println("password = " + password);
  String t = rc.getValue("test");
  System.out.println("t = " + t);
  //测试保存saveFile
  try {
rc.setValue(password, t);
} catch (IOException e) {
// TODO Auto-generated catch blo