您的位置:首页 > 运维架构

读取properties文件并遍历输出

2016-07-11 00:00 393 查看
读取properties文件,生成map,并遍历输出所有的key--value

package com;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;

public class ReadProperties {

private static Map<Object, Object> contains = new HashMap<Object, Object>();
private static Properties p = null;

public static void main(String[] args) {
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream("w:/printContract.properties"));
p = new Properties();
p.load(in);
contains = p;

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}

int i = 1;
for (Entry<Object, Object> item : contains.entrySet()) {
System.out.println("第"+i+"个key是:"+item.getKey());
System.out.println("第"+i+"个value是:"+item.getValue());;
i++;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: