operties檔案的讀取、新增、修改、清空、另存

daditao發表於2014-01-06
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/**
 * 操作properties檔案
 * @author Administrator
 *
 */
public class Configuration{
 
    private Properties propertie;
    private String path;
    private FileInputStream inputFile;
    private FileOutputStream outputFile;
  
   /**
    * 構造器
    * 用途:新建一個properties檔案
    * 呼叫setValue(key,value)新增節點
    * 呼叫saveAsFile(路徑,描述)方法儲存
    */
    public Configuration(){
        propertie = new Properties();
    }
  
   /**
    * 構造器(輸入路徑)
    * @param filePath properties檔案路徑
    * 呼叫setValue(key,value)新增節點
    * 呼叫getValue(key)獲取節點的值
    * 呼叫clear()方法清空properties檔案
    * 呼叫saveFile()儲存
    */
    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();
        }
    }
  
   /**
    * 通過key值獲取value
    * @param key
    * @return
    */
    public String getValue(String key){
        if(propertie.containsKey(key)){
            String value = propertie.getProperty(key);//得到某一屬性的值
            return value;
        }
        else
            return "";
    }
  
   /**
    * 清空properties檔案
    *
    */
    public void clear(){
        propertie.clear();
    }
  
   /**
    * 輸入key和value進行新增節點 ------構造器(輸入路徑)例項化時候 呼叫
    * @param key
    * @param value
    */
    public void setValue(String key, String value){
        propertie.setProperty(key, value);
    }
   
    /**
     * 儲存properties檔案
     * @param fileName
     * @param description
     */
     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();
         }
     }
  
   /**
    * 另存為properties檔案
    * @param fileName
    * @param description
    */
    public void saveAsFile(String fileName, String description){
        try {
            outputFile = new FileOutputStream(fileName);
            propertie.store(outputFile, description);
            outputFile.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException ioe){
            ioe.printStackTrace();
        }
    }
  
    /**
     * 測試
     * @param args
     */
    /*public static void main(String[] args){
        Configuration rc = new Configuration("d://test.properties");
      
        //測試從檔案讀取
        String databaseName = rc.getValue("databaseName");
        String dbUserName = rc.getValue("dbUserName");
        String dbPort = rc.getValue("dbPort");
        String dbPassword = rc.getValue("dbPassword");
      
        System.out.println("databaseName = " + databaseName);
        System.out.println("dbUserName = " + dbUserName);
        System.out.println("dbPort = " + dbPort);
        System.out.println("dbPassword = " + dbPassword);
       
        //測試儲存saveFile
        rc.setValue("test", "000000");
        rc.saveFile("saveFile");
       
       
        //測試另存為saveAsFile
        Configuration cf = new Configuration();
        cf.setValue("min", "0");
        cf.setValue("max", "99999");
        cf.saveAsFile("d://testSaveAsFile.properties", "saveAsFile");
 
    }*/
}


相關文章