Java基础知识
时间:2014-10-08 10:28:15
收藏:0
阅读:170
1、Properties
(1)通过资源包ResourceBundle获得资源对象
<pre name="code" class="java">public class PropertiesTest {
public static HashMap<String, Properties> hashMap = new HashMap<String, Properties>();
public static String filename = "my";
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ResourceBundle bundle = ResourceBundle.getBundle("my");
Properties properties = new Properties();
Enumeration<String> enumeration = bundle.getKeys();
while (enumeration.hasMoreElements()) {
String key = enumeration.nextElement();
String value = bundle.getString(key);
properties.put(key, value);
}
hashMap.put(filename, properties);
Properties properties2 = hashMap.get(filename);
Enumeration<Object> enumeration2 = properties2.keys();
while (enumeration2.hasMoreElements()) {
String key = (String) enumeration2.nextElement();
String value1 = properties2.getProperty(key);
try {
String value2 = new String(value1.getBytes("ISO8859-1"),
"UTF-8");
System.out.println("key-->"
+ new String(key.getBytes("ISO8859-1"), "UTF-8")
+ " value-->" + value2);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
(2)通过输入流获得资源对象
InputStream inputStream = getClass().getResourceAsStream(
"/my.properties");
Properties properties = new Properties();
try {
properties.load(inputStream);
Enumeration<Object> enumeration = properties.keys();
while (enumeration.hasMoreElements()) {
String key = (String) enumeration.nextElement();
String value = properties.getProperty(key);
System.out.println(new String(key.getBytes("ISO8859-1"),
"utf-8")
+ ";"
+ new String(value.getBytes("ISO8859-1"), "utf-8"));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
评论(0)