Java traversal method instance for all elements of properties _java

Source: Internet
Author: User

Copy Code code as follows:

Initialize Properties

Properties Pro = new properties ();

try {
InputStream inStr = Classloader.getsystemresourceasstream ("wahaha.properties");
Pro.load (INSTR);
catch (FileNotFoundException e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}


PropertyNames () Returns an enumeration of all keys in the property list

Copy Code code as follows:

Enumeration Enu2=pro.propertynames ();
while (Enu2.hasmoreelements ()) {
String key = (string) enu2.nextelement ();
SYSTEM.OUT.PRINTLN (key);
}

Returns all the property values

Copy Code code as follows:

Properties inherit from the Hashtable,elements () is the Hashtable method that returns an enumeration of the values in the hash table.
Enumeration enu=pro.elements ();
while (Enu.hasmoreelements ()) {
String key = (string) enu.nextelement ();
SYSTEM.OUT.PRINTLN (key);
}

Returns all attributes (property name, property value)

Copy Code code as follows:

Properties inherited from Hashtable,entryset () is a Hashtable method,
Returns the Set view of the keys contained in this Hashtable. Each element in this collection is a map.entry
Iterator It=pro.entryset (). iterator ();
while (It.hasnext ()) {
Map.entry entry= (Map.entry) it.next ();
Object key = Entry.getkey ();
Object value = Entry.getvalue ();
SYSTEM.OUT.PRINTLN (key + ":" +value);
}

Suppose the content in Wahaha.properties is:
------------------------------
Name1=xxxx
Name2=yyyyy
Name3=zzzzzzz
------------------------------

The above code will output:
--------------------------
Name1
Name2
Name3
Xxxx
Yyyyy
Zzzzzzz
Name1:xxxx
Name2:yyyyy
Name3:zzzzzzz
---------------------------------

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.