Java读写properties配置文件

Java读写配置文件注意以下几点:

1. 配置文件config.properties在src目录下,编译后会在classes目录下

2. 通常写入不了一般都是路径错误,写到其他目录去了。

3.JDK Properties 类读取的文件是不允许有非 ASCII 码字符的,文件需要使用 JDK 的 native2ascii 工具转换一下

package com.util;

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

/**
 * 功能:读写配置文件
 * @author Adam
 *
 */
public class PropertyHelper {
	static String profilepath = PropertyHelper.class.getClassLoader().
	getResource("").getPath() + "config.properties";
    private static Properties props = new Properties();
    static {
        try {
        	System.out.println("配置文件路径:"+profilepath);
        	InputStream is = new BufferedInputStream (new FileInputStream(profilepath));
        	props.load(is);
	        is.close(); 
        }catch (IOException e) {
        	e.printStackTrace();
        }
    }
    
    /**
    * 功能:根据key获得value
    * @param key:key
    * @return String:value
    */
    public static String getValueByKey(String key) {
        return props.getProperty(key);
    }
    
    /**
    * 功能:根据属性文件路径和key读取value
    * @param filePath:属性文件路径
    * @param key:键名
    */ 
    public static String readValue(String filePath, String key) {
        Properties props = new Properties();
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(filePath));
            props.load(in);
            return props.getProperty(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * 功能:写入properties文件的键值对,key存在,则修改,key不存在则添加
     * @param keyname:key
     * @param keyvalue:value
     */
    public static void writeProperties(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("属性文件写入异常");
        }
    }
    
    /**
     * 功能:读取properties的全部信息
     * @param filePath:属性文件路径
     */
    public static void readProperties(String filePath) {
    	Properties props = new Properties();
        try {
        	InputStream is = new BufferedInputStream (new FileInputStream(filePath));
        	props.load(is);
            Enumeration en = props.propertyNames();
            while (en.hasMoreElements()) {
            	String key = (String) en.nextElement();
                String Property = props.getProperty (key);
                System.out.println(key + "=" + Property);
            }
        } catch (Exception e) {
        	e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
    	System.out.println(PropertyHelper.getValueByKey("SYSTEM_SMTP"));
    	PropertyHelper.writeProperties("SYSTEM_SMTP","中文");
    	readProperties(profilepath);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码上富贵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值