properties操作。

来源:互联网 发布:互联网公平台用java 编辑:程序博客网 时间:2024/06/06 11:04

1.读取properties

假设properties文件在项目跟目录/classes/path.properties

String rootPath=this.getClass().getClassLoader().getResource("/").getPath()+"path.properties";

Properties pps = new Properties();InputStream in = new BufferedInputStream(new FileInputStream(rootPath));pps.load(in);

2.遍历

/** *  */package pkg;import java.util.Enumeration;import java.util.Iterator;import java.util.Map.Entry;import java.util.Properties;/** * @author qefee *  */public class ShowProperties {/** * @param args */public static void main(String[] args) {Properties properties = System.getProperties();// show keysshowKeys(properties);// show valuesshowValues(properties);// show keys and valuesshowKeysAndValues(properties);}/** * @param properties */private static void showKeys(Properties properties) {Enumeration<?> enu = properties.propertyNames();while (enu.hasMoreElements()) {Object key = enu.nextElement();System.out.println(key);}}/** * @param properties */private static void showValues(Properties properties) {Enumeration<Object> enu = properties.elements();while (enu.hasMoreElements()) {Object value = enu.nextElement();System.out.println(value);}}/** * @param properties */private static void showKeysAndValues(Properties properties) {Iterator<Entry<Object, Object>> it = properties.entrySet().iterator();while (it.hasNext()) {Entry<Object, Object> entry = it.next();Object key = entry.getKey();Object value = entry.getValue();System.out.println("key   :" + key);System.out.println("value :" + value);System.out.println("---------------");}}}


0 0
原创粉丝点击