Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ The Java Properties class action     - OpenResty load balancing MySQL (Database)

- Getting Started with Linux system to learn: How do I know which processes are running on the CPU core (Linux)

- How to install Bugzilla 4.4 on Ubuntu / CentOS 6.x (Linux)

- Improve the efficiency of Linux development tools 5 (Linux)

- MySQL combination Git achieve differential backups can be used in the production environment (Database)

- Android custom ViewPager create kaleidoscopic image transition effects (Programming)

- ssh port forwarding Comments (Server)

- Ubuntu 14.04 build Hadoop 2.5.1 standalone and pseudo-distributed environment (32-bit) (Server)

- Ubuntu to install systems Indicator Sticky Notes 0.4.4 (Linux)

- Use JMS listener Oracle AQ, trigger the execution of Java programs in the database changes (Database)

- Ubuntu rights management common commands (Linux)

- The free command in Linux (Linux)

- File permissions under Linux (Linux)

- Apache Spark1.1.0 deployment and development environment to build (Server)

- Nginx version information hidden or modified (Server)

- Caffe install under Ubuntu 14.04 64bit (Linux)

- Redis application of Sina Weibo (Database)

- Git build a team development environment operating drills (Linux)

- Monitoring Linux System 7 command-line tool (Linux)

- Oracle archive log deletion (Database)

 
         
  The Java Properties class action
     
  Add Date : 2018-11-21      
         
         
         
  Java has a more important category: Properties class, which is mainly used for reading java configuration file. Each language supported by its own configuration file. java configuration often * .perperties file, the file format is text format, content format as "Kin = value" pair format, text annotations available # comment.

Properties inherited Hashtable

It provides several main methods:

1. getProperty (String key), search property in this property list with the specified key. That is the parameter key, get key corresponding to the value.

2. load (InputStream inStream), Reads a property list (key and element pairs) from the input stream. Through the specified file (for example, above test.properties file) loaded to get all the keys in the file - value pairs. For getProperty (String key) to search.

3. setProperty (String key, String value), method calls the Hashtable put. He set the key by calling the put method of the base class - value pairs.

4. store (OutputStream out, String comments), suitable for using the load method for loading into a Properties table format, this Properties table in the property list (key and element pairs) to the output stream. Instead, the key method and load method - go to the value written to the specified file.

5. clear (), remove all loaded key - value pairs. The method provides in the base class.

Two, Java Properties file read

    There are many Java Properties file read method

But the most common is through getResourceAsStream java.lang.Class class (String name) method to achieve, you can call as follows:

InputStream in = getClass () getResourceAsStream ( "Resource Name");. As we write the program, enough with this one kind.

This is also commonly used, or the following:

InputStream in = new BufferedInputStream (new FileInputStream (filepath));

Third, relevant examples

Here are a few examples to deepen understanding and memory Properties class.

We know that, Java Virtual Machine (JVM) has its own system configuration file (system.properties), we can obtain the following manner.

1, to obtain the JVM system properties

public class Property {
    public static void main (String args []) {
        System.out.println (new Date ());
        Properties p = System.getProperties ();
        p.list (System.out); // print all properties about JVM
    }

}

2. Read the local profile and output test.properties

public class Property {
    public static void main (String args []) {
// System.out.println (new Date ());
// Properties p = System.getProperties ();
// P.list (System.out); // print all properties about JVM
        
        Properties text = new Properties ();
        try {
            text.load (new FileInputStream ( "E: \\ workspace \\ ch01 \\ text.properties"));
        } Catch (FileNotFoundException e) {
            e.printStackTrace ();
        } Catch (IOException e) {
            e.printStackTrace ();
        }
        
        @SuppressWarnings ( "Rawtypes")
    / * Enumeration interfaces also java.util package that is functionally similar to Iterator are used to traverse the elements in the collection
        But the enumeration Enumeration provided only through the Vector and Hashtable collection element type of function * /
        Enumeration num = text.propertyNames (); // get the contents of the file in the Enumeration accessories on the key
        
        while (num.hasMoreElements ()) {
            String key = (String) num.nextElement ();
            String value = text.getProperty (key);
            System.out.println (key + "=" + value);
        }
    }

}

result:

age = 23
name = linyuhuan
weight = 140
long = 212

3, a more comprehensive examples

Read the value based on key

Read all the information on properties

Write new properties Information

package ch01;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;

/ **
 * Common operations on Properties class
 * /
public class TestProperties {

    public static void main (String [] args) throws IOException {
            WriteProperties ( "E: \\ workspace \\ ch01 \\ text.properties", "add2", "2121");
    }
    
    // Read the value based on key
    public static String GetValueByKey (String filePath, String key) {
        Properties pps = new Properties ();
        try {
            InputStream in = new BufferedInputStream (new FileInputStream (filePath));
            pps.load (in);
            String value = pps.getProperty (key);
            System.out.println (key + "=" + value);
            return value;
        } Catch (IOException e) {
            e.printStackTrace ();
            return null;
        }
    }
    
    // Read all the information profile
    @SuppressWarnings ( "Rawtypes")
    public static void GetAllProperties (String filePath) throws IOException {
        Properties pps = new Properties ();
        InputStream in = new BufferedInputStream (new FileInputStream (filePath));
        pps.load (in);
        Enumeration num = pps.propertyNames (); // Get all the properties in the configuration file name enumeration
        while (num.hasMoreElements ()) {
            String key = (String) num.nextElement ();
            String value = pps.getProperty (key);
            System.out.println (key + "=" + value);
        }
    }
    
    // Write information Properties
    public static void WriteProperties (String filePath, String pKey, String pValue) throws IOException {
        Properties pps = new Properties ();
        InputStream in = new FileInputStream (filePath);
        pps.load (in);
        OutputStream out = new FileOutputStream (filePath);
        pps.setProperty (pKey, pValue);
        pps.store (out, "Update" + pKey + "name");
    }
}

Results: test.properties file

#Update Add2 name
#Tue Jun 30 17:07:55 CST 2015
age = 23
name = linyuhuan
weight = 140
add2 = 2121
long = 212
     
         
         
         
  More:      
 
- shell script: MySQL monitoring service is normal (Database)
- N1QL bring SQL-like query NoSQL database experience (Database)
- Nginx supports user multi-threaded downloads and resume broken (Server)
- Oracle 11g creates virtual private directory RMAN-06004 ORA-00942 error handling (Database)
- MySQL5.7 implement virtual column expression index (Database)
- Python decorators to learn and practice the actual usage scenarios (Programming)
- Use Ambari rapid deployment Hadoop big data environment (Server)
- 8 Docker knowledge you may not know (Server)
- OpenDJ installed on RHEL6 (Linux)
- Use Linux built-in firewall to improve network access control (Linux)
- Linux landing problem (Linux)
- Oracle 11g DataGuard service to start automatically (Database)
- Jetty JNDI Development combat (Linux)
- Linux regex sed detailing (Linux)
- ORA-00600 error solve one case (Database)
- Java MVC CRUD examples (Programming)
- Linux operating system must know the security command (Linux)
- Three kinds of implementation model of the Linux thread history (Programming)
- Oracle Shared Server Configuration (Database)
- Thinking in Java study notes - initialization and cleanup (Programming)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.