用JAVA轻松操作properties文件 (Read and Write Properties File)

用JAVA轻松操作properties文件 (Read and Write Properties File)


不用介绍了, 所有的代码都写在这里了, 希望这个对你们有用。

You can access to a set of useful methods for reading and writing properties files in the code below:


import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;

/**
 *
 * @author Fei
 */
public class PropertyUtils {
   
    public PropertyUtils(){}
   
    //1). Read value based on the value of key
    public String readValue(String filePath,String key) {
        Properties props = new Properties();
            try{
                InputStream in = new BufferedInputStream (new FileInputStream(filePath));
                props.load(in);
                String value= props.getProperty(key);
                System.out.println("Class:PropertyUtils, Method:readValue, key:"+key+",value:"+value);
                return value;
            }catch(Exception ex){
                ex.printStackTrace();              
                return null;
            }
    }
    //2). Reads an enumeration of keys from a file
    public  Enumeration getKeys(String filePath){
        Enumeration en;
        Properties props = new Properties();
        try{
            InputStream in = new BufferedInputStream(new FileInputStream(filePath));
            props.load(in);
            en  = props.propertyNames();
            /*
            while(en.hasMoreElements()){
                String key = (String)en.nextElement();
                String property = props.getProperty(key);
                //for testing purpose
                System.out.println(key+property);
               return en;
            }
             * */
          return en;
        }catch(Exception ex){
            ex.printStackTrace();
          return null;
        }
    }
    //3). get the all the keys and values as a Object Properties
    public Properties readProperties(String filePath){
        Properties props = new Properties();
        try{
            InputStream in = new BufferedInputStream(new FileInputStream(filePath));
            props.load(in);
            return props;
        }catch(Exception ex){
            Debug.log(this, "readProperties", "Error occured during loading property file");
            return null;
        } 
    }
   
    //4). Write to specific property file
    public void writeProperties(String filePath,String parameterKey,String parameterValue){
        Debug.log(this, "wirteProperty", "File Path:"+filePath);
        Properties prop = new Properties();
        try{
            InputStream fis = new FileInputStream(filePath);
            //read key value pair from file input stream
            prop.load(fis);
            OutputStream fos = new FileOutputStream(filePath);
            /*
             * Calls the Hashtable method put. Provided for parallelism with the getProperty method.
             * Enforces use of strings for property keys and values.
             * The value returned is the result of the Hashtable call to put.
             *
             * */
            prop.setProperty(parameterKey, parameterValue);
            /*
             * Writes this property list (key and element pairs) in this Properties table to
             * the output stream in a format suitable for loading into a Properties table
             * using the load method
             * */
            prop.store(fos, "Last Update, key:"+parameterKey+",value: "+parameterValue);
           
        }catch(Exception ex){
             System.err.println("Visit "+filePath+" for updating "+parameterKey+" value error");
        }
    }
   
    //5). Remove one record based on the key specified
    public void removeProperty(String filePath,String key){
        Debug.log(this, "removeProperty", "File Path:"+filePath);
        Properties props = new Properties();
        try{
            InputStream in = new BufferedInputStream(new FileInputStream(filePath));
            props.load(in);
            props.remove(key);
            //create a new file output stream to hold the property file in which
            //entry has been deleted.
            OutputStream fos =  new FileOutputStream(filePath);
            props.store(fos, "Last Update,"+new Date()+" Deletion: key:"+key);
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
   
    //6). Update record based on key specified
    public void updateProperty(String filePath,String key,String value){
        Debug.log(this, "removeProperty", "File Path:"+filePath);
        Properties props = new Properties();
       
        try{
            InputStream in =  new BufferedInputStream(new FileInputStream(filePath));
            props.load(in);
            props.setProperty(key, value);
            OutputStream fos = new FileOutputStream(filePath);
            props.store(fos, "Last Update, key:"+key+",value: "+value);
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
   
    public boolean checkKeyAvailability(String filePath, String key){
        boolean flag = true;//By Default all keys all available
        Enumeration keys = this.getKeys(filePath);
        while(keys.hasMoreElements()){
            String keyStr = (String)keys.nextElement();
            if(keyStr.equalsIgnoreCase(key)){
                flag=false;
            }
        }
       
        return flag;
    }
    /*
     public static void main(String[] args) {
        //readValue("info.properties","url");
        //writeProperties("info.properties","age","21");
        readProperties("c://java//info.properties" );
        System.out.println("OK");
    }*/

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值