1、工程结构
2、ConfigFileTest.java
package com.configfile; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.Properties; public class ConfigFileTest { private static Properties config = null; static { InputStream in = ConfigFileTest.class.getClassLoader().getResourceAsStream("parameter.properties"); config = new Properties(); try { config.load(in); in.close(); } catch (IOException e) { //读取url.properties出错 e.printStackTrace(); } } /** * 通过键获得对应的值 * @param key * @return */ public static String getValue(String key) { try { String value = config.getProperty(key); return value.trim(); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 读取properties的全部信息 */ public static void getAllProperties() { try { Enumeration en = config.propertyNames(); while (en.hasMoreElements()) { String key = (String) en.nextElement(); String Property = config.getProperty(key); System.out.println("key = "+config.getProperty(key)); } } catch (Exception e) { e.printStackTrace(); System.err.println("ConfigInfoError" + e.toString()); } } public static void main(String args[]) { System.out.println(getValue("title")); getAllProperties(); } }
3、parameter.properties
title=\u4E2D\u56FD\u4F60\u597D ##爱你中国 message=\u6211\u6765\u4E86 ##我来了
时间: 2024-10-13 00:37:29