There is a relatively important class Properties (Java.util.Properties), which is mainly used to read Java configuration files. Various languages have their own supported configuration files. Many variables in the configuration files are often changed. This is also to facilitate users and allow users to modify related variable settings without the program itself. Today, we will start using Properties.
Use of Properties in Java
Properties documentation description:
The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.
Description of Properties class:
public class Properties extends Hashtable<Object,Object>
The project structure of the test is as follows:
1. In the huhx.properties file, we add a piece of data for convenience:
name=huhx
2. Load and read the huhx.properties file to get the corresponding properties
Properties properties = new Properties();FileInputStream fis = new FileInputStream("huhx.properties");properties.load(fis);System.out.println(properties.get("name")); 3. Use of Properties' list method
PrintStream printStream = System.out;properties.list(printStream);
Specific code of list method:
public void list(PrintStream out) {out.println("-- listing properties --");Hashtable h = new Hashtable();enumerate(h);for (Enumeration e = h.keys() ; e.hasMoreElements() ;) {String key = (String)e.nextElement();String val = (String)h.get(key);if (val.length() > 40) {val = val.substring(0, 37) + "...";}out.println(key + "=" + val);}} 4. Use of Properties store method
OutputStream outputStream = new FileOutputStream("huhx.txt");properties.store(outputStream, "comments"); 5. Use of Properties' storeToXML method
OutputStream outputStream2 = new FileOutputStream("huhx.xml");properties.storeToXML(outputStream2, "comments"); 6. The final generated file is as follows:
huhx.txt:
#comments#Thu May 19 19:19:36 CST 2016name=huhx
huhx.xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"><properties><comment>comments</comment><entry key="name">huhx</entry></properties>
Friendly links, PropertiesTest.java:
package com.huhx.linux;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.OutputStream;import java.io.PrintStream;import java.util.Properties;public class PropertiesTest {public static void main(String[] args) throws Exception {// General use of Properties Properties properties = new Properties();FileInputStream fis = new FileInputStream("huhx.properties");properties.load(fis);System.out.println(properties.get("name"));// The following is the part of the test PrintStream printStream = System.out;properties.list(printStream);OutputStream outputStream = new FileOutputStream("huhx.txt");properties.store(outputStream, "comments");OutputStream outputStream2 = new FileOutputStream("huhx.xml");properties.storeToXML(outputStream2, "comments");}} The above is a detailed explanation of the use of Properties in Java introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!