天天看點

java操作.properties屬性檔案

package test;

import java.io.BufferedInputStream;  

import java.io.File;  

import java.io.FileInputStream;  

import java.io.FileNotFoundException;  

import java.io.FileOutputStream;  

import java.io.IOException;  

import java.io.InputStream;  

import java.io.OutputStream;  

import java.util.Properties;  

public class PropertyHelper {  

    //屬性檔案的路徑  

    static String profilepath="C:/work/Pro/src/pro.properties";  

    static String Object = "object";  

    private static Properties props = new Properties();  

//    private static Properties proNum = new Properties();  

    static {

        File file=new File(profilepath);  

        if(!file.exists()){  

            try {  

                file.createNewFile();  

            } catch (IOException e) {  

                // TODO Auto-generated catch block  

                e.printStackTrace();  

            }  

        }  

        try {  

            props.load(new FileInputStream(profilepath));  

        } catch (FileNotFoundException e) {  

            e.printStackTrace();  

            System.exit(-1);  

        } catch (IOException e) {          

            System.exit(-1);  

        }  

    }  

    public static String getKeyValue(String key) {

        Properties prop = new Properties();  

        try {

            prop.load(new FileInputStream(profilepath));

        } catch (FileNotFoundException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }  

        return prop.getProperty(key);  

    }  

    public static 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);  

            return value;  

        } catch (Exception e) {  

            e.printStackTrace();  

            return null;  

        }  

    }  

    public static void writeProperties(String keyname,String keyvalue) {    

        try {  

            OutputStream fos = new FileOutputStream(profilepath);  

            props.setProperty(keyname, keyvalue);  

            props.store(fos, "Update '" + keyname + "' value");  

        } catch (IOException e) {  

            System.err.println("屬性檔案更新錯誤");  

        }  

    }  

    public static void updateProperties(String keyname,String keyvalue) {  

        try {  

            props.load(new FileInputStream(profilepath));  

            OutputStream fos = new FileOutputStream(profilepath);              

            props.setProperty(keyname, keyvalue);  

            props.store(fos, "Update '" + keyname + "' value");  

        } catch (IOException e) {  

            System.err.println("屬性檔案更新錯誤");  

        }  

    }

    public static void main(String[] args) {

        System.out.println(getKeyValue("LaSaPhone"));

    }

}