Java遍历Properties的所有的元素加载
Properties properties = new Properties();
properties.load(PlayGround.class.getClassLoader().getResourceAsStream("tag.properties"));
输出
/**
* 输出properties的key和value
*/
public static void printProp(Properties properties) {
System.out.println("---------(方式一)------------");
for (String key : properties.stringPropertyNames()) {
System.out.println(key + "=" + properties.getProperty(key));
}
System.out.println("---------(方式二)------------");
Set keys = properties.keySet();//返回属性key的集合
for (Object key : keys) {
System.out.println(key.toString() + "=" + properties.get(key));
}
System.out.println("---------(方式三)------------");
Set> entrySet = properties.entrySet();//返回的属性键值对实体
for (Map.Entry entry : entrySet) {
System.out.println(entry.getKey() + "=" + entry.getValue());
}
System.out.println("---------(方式四)------------");
Enumeration> e = properties.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
String value = properties.getProperty(key);
System.out.println(key + "=" + value);
}
}
/*** 输出properties的key和value
*/
public static voidprintProp(Properties properties) {
System.out.println("---------(方式一)------------");
for(String key : properties.stringPropertyNames()) {
System.out.println(key + "=" +properties.getProperty(key));
}
System.out.println("---------(方式二)------------");
Set keys = properties.keySet();//返回属性key的集合
for(Object key : keys) {
System.out.println(key.toString() + "=" +properties.get(key));
}
System.out.println("---------(方式三)------------");
Set> entrySet = properties.entrySet();//返回的属性键值对实体
for (Map.Entryentry : entrySet) {
System.out.println(entry.getKey() + "=" +entry.getValue());
}
System.out.println("---------(方式四)------------");
Enumeration> e =properties.propertyNames();
while(e.hasMoreElements()) {
String key =(String) e.nextElement();
String value =properties.getProperty(key);
System.out.println(key + "=" +value);
}
}
jar包读取包内properties文件
properties位于src目录下
project
--src
-----package
-----test.properties
Properties p = newProperties();
//从输入流中读取属性列表(键和元素对)
p.load(DB.class.getClass().getResourceAsStream("/test.properties"));
String testKey = p.getProperty("testKey");
不能这样写
File file = new File("src/test.properties");
输出路径为会带! 因为是jar包
java指定编码格式utf-8运行jar
java -Dfile.encoding=utf-8 -jar test.jar