1. Introduction to Properties
The Properties class inherits from HashTable and provides methods that are very similar to the Map implementation class HashMap. It was early in Java programming and had little change. The Tiger version of J2SE enhances this class not only uses it to specify multiple key-value pairs separated by equal signs in a single line (where keys and values are separated by equal signs), but also to load and save these key-value pairs in an XML file.
2. Loading and simple use of Properties
1.Path storage
2. Information configuration
name = Lilyage = 22
3. Simple loading and use
package com.my.utils;import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class LoadProp { public static void main(String[] args) { // TODO Auto-generated method stub Properties prop = new Properties(); InputStream in = LoadProp.class.getClassLoader().getResourceAsStream("config/prop.properties"); try { prop.load(in); System.out.println(prop.get("name")); prop.setProperty("name", "Lucy"); System.out.println(prop.get("name")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}4. Use java.util.Enumeration to get all keys
package com.my.utils;import java.io.IOException;import java.io.InputStream;import java.util.Enumeration;import java.util.Properties;public class LoadProp { @SuppressWarnings("unchecked") public static void main(String[] args) { // TODO Auto-generated method stub Properties prop = new Properties(); InputStream in = LoadProp.class.getClassLoader().getResourceAsStream("config/prop.properties"); Enumeration<String> en = null; String key = null; try { prop.load(in); en = (Enumeration<String>) prop.propertyNames(); while (en.hasMoreElements()) { key = (String) en.nextElement(); System.out.println(key); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.