首页 > 技术文章 > java对配置文件properties的操作

wang-liang-blogs 2019-07-04 11:22 原文

1.读取配置文件的键值对,转为Properties对象;将Properties(键值对)对象写入到指定文件。

package com.ricoh.rapp.ezcx.admintoolweb.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PropertiesFileHandle {

    private static Logger logger = LoggerFactory.getLogger(PropertiesFileHandle.class);

    public static Properties readProperties(String filePath) {
        String realPath = FileUtil.getEzChargerInstallPath() + filePath;
        
        Properties props = new Properties();
        File configFile = new File(realPath);
        logger.debug("#configFile: " + configFile.getAbsolutePath());
        InputStream fis = null;
        try {
            fis = new FileInputStream(configFile);
            props.load(fis);
        } catch (IOException e) {
            logger.error("readProperties failed in" + realPath + ". "+ e.toString());
            return null;
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (Exception e) {
                logger.debug("readProperties close file failed." + e.toString());
            }
        }
        return props;
    }

    public static boolean writeProperties(String filePath, Properties prop) {
        String realPath = FileUtil.getEzChargerInstallPath() + filePath;
        
        File configFile = new File(realPath);
        if(!configFile.exists()) {
            configFile.getParentFile().mkdirs();
            try {
                configFile.createNewFile();
            } catch (IOException e) {
                logger.error("PropertiesFileHandle.writeProperties failed. because create file[" + realPath
                        + "]. is IOException:"+ e.getMessage());
                e.printStackTrace();
                return false;
            }
        }
        InputStream fis = null;
        OutputStream fos = null;
        try {
            fos = new FileOutputStream(configFile);
            prop.store(fos, "");
        } catch (Exception e) {
            logger.error("WriteProperties failed in" + realPath + ". "+ e.toString());
            return false;
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (Exception e) {
                logger.debug("writeProperties close file failed." + e.toString());
            }
        }
        return true;
    }
}

2.通过输入流或者Properties对象将Properties文件的内容读取到map集合中。

package com.ricoh.rapp.ezcx.edcactivation.internal;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;

public class PropertyUtil {
    public static Map<String, String> loadPropertiesFile(InputStream file) {
        HashMap result = new HashMap();

        try {
            Properties prop = new Properties();
            prop.load(file);
            Iterator var4 = prop.entrySet().iterator();

            while (var4.hasNext()) {
                Entry<Object, Object> entry = (Entry) var4.next();
                result.put((String) entry.getKey(), (String) entry.getValue());
            }
        } catch (IOException var5) {
            System.out.println("faild load properties file .");
        }

        return result;
    }

    public static Map<String, String> loadPropertiesFile(Properties prop) {
        Map<String, String> result = new HashMap();
        if (prop == null) {
            return result;
        } else {
            Iterator var5 = prop.entrySet().iterator();

            while (var5.hasNext()) {
                Entry<Object, Object> entry = (Entry) var5.next();
                String key = (String) entry.getKey();
                String value = (String) entry.getValue();
                if (key != null && key.length() > 0 && value != null && value.length() > 0) {
                    result.put(key, value);
                }
            }

            return result;
        }
    }
}

3.实例使用1和2的方式来处理Properties文件

private void innitPropreties() {
        Map<String, String> rsiConfMap = new HashMap<>();
        Properties proxyProp = PropertiesFileHandle.readProperties("/conf/test.properties");
        if (proxyProp != null) {
            rsiConfMap = PropertyUtil.loadPropertiesFile(proxyProp);
        }else {
            rsiConfMap.put("key1", "value1");
            rsiConfMap.put("key2", "value2");
            Properties properties = new Properties();
            properties.put("key1", "value1");
            properties.put("key2", "value2");
            PropertiesFileHandle.writeProperties("/conf/test.properties", properties);
        }
        
        String value1= rsiConfMap.get("key1");
        String value2= rsiConfMap.get("key2");
    }

 

推荐阅读