首页 > 编程语言 > 详细

Java基础知识

时间:2014-10-08 09:47:25      阅读:329      评论:0      收藏:0      [点我收藏+]

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基础知识

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!