Basic Java Knowledge

Source: Internet
Author: User

1. Properties

(1) Obtaining resource objects through resource bundle ResourceBundle

<pre name= "code" class= "Java" >public class Propertiestest {public static hashmap<string, properties> HashMap = new hashmap<string, properties> ();p ublic static String filename = "my";/** * @param args */public static V OID Main (string[] args) {//TODO auto-generated method Stubresourcebundle 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);p roperties.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 Blocke.printstacktrace ();}}}


(2) Obtaining a resource object from an input stream

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


Basic Java Knowledge

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.