java中对配置文件properties读取

来源:互联网 发布:歌莉娅淘宝官方旗舰店 编辑:程序博客网 时间:2024/06/03 21:01
package com.test.propertyFile;import java.io.IOException;import java.io.InputStream;import java.io.UnsupportedEncodingException;import java.util.Properties;public class Test {/** * 通过key获取文件中的value * 注意: * 1、文件的路径 * 2、文件的编码格式 * @param key * @return */public static String getValueByKey(String key, String filePath){Properties prop = new Properties();InputStream inStream = Test.class.getResourceAsStream(filePath);try {prop.load(inStream);} catch (IOException e) {e.printStackTrace();System.out.println("读取文件异常");return null;}String value = null;try {value = new String(prop.getProperty(key).getBytes("iso-8859-1"),"utf-8");} catch (UnsupportedEncodingException e) {e.printStackTrace();System.out.println("读取数值异常");}return value;}public static void main(String[] args) {String value = Test.getValueByKey("name", "test.properties");System.out.println(value);}}

原创粉丝点击