码迷,mamicode.com
首页 > 编程语言 > 详细

Java基础知识

时间:2014-10-08 10:28:15      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:java   技术   高级   class   object   

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();
		}
	


Java基础知识

标签:java   技术   高级   class   object   

原文地址:http://blog.csdn.net/scboyhj__/article/details/39889145

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!