Home  >  Article  >  Java  >  A brief introduction to Properties in Java

A brief introduction to Properties in Java

黄舟
黄舟Original
2017-05-28 09:21:401891browse

There is a relatively important class Properties (Java.util.Properties) in Java, which is mainly used to read Java's configuration file. Each language has its own supported configuration files and configuration files. Many of the variables are frequently changed. This is also done for the convenience of users, allowing users to modify related variable settings without the program itself.

Properties class in Java Operation

If you learn knowledge without using it, it is useless. You will have to learn it all over again when you actually use it. Recently I was looking at the source code of several open source simulators, which involved a lot of references to the Properties class. Since Java has not been used for a long time, most of these simulators are written in Java, plus some scripting languages. ##Python,Perl and the like, I have to pick them up again as a last resort.

1. Java Properties class

There is a relatively important class Properties (Java.util.Properties) in Java, which is mainly used for reading Taking the Java configuration file, each language has its own supported configuration file. Many variables in the configuration file are frequently changed. This is also for the convenience of users, allowing users to modify related variable settings without leaving the program itself. The configuration file supported by Python is the .ini file. Similarly, it also has its own class ConfigParse for reading configuration files, which is convenient for programmers or users to modify the .ini configuration file through this class method. In Java, its configuration file is often a .properties file in the format of a text file. The content of the file is in the format of "key=value". The text annotation

information can be annotated with "#".

Properties classInherits from Hash

table, as follows:


It provides There are several main methods:

1. getProperty (

String key

),

searches for a property in this property list using the specified key. That is, through the parameter key, the value corresponding to key is obtained. 2. load (InputStream inStream), reads a list of properties (key and element pairs) from the input stream. Get all key-value pairs in a specified file (say the test.properties file above) by loading it. For getProperty (String key) to search. 3. set

Property (String key, String value), call the Hashtable method put. It sets the key-value pairs by calling the put method of the base class.

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

5. clear

(), clears all loaded key-value pairs. This method is provided in the base class.

2. Java reads Properties files

There are many ways to read Properties files in Java, but the most commonly used one is through java .lang.Class

class’s get

ResourceAsStream(String name) method is implemented. It can be called as follows:

InputStream in = getClass().getResourceAsStream("资源Name");作为我们写程序的,用此一种足够。
or the following Also commonly used:
InputStream in = new BufferedInputStream(new FileInputStream(filepath));

3. Related examples

The following are several examples to deepen the understanding and memory of the Properties class. We know that the Java Virtual Machine (JVM) has its own system configuration file (system.properties), which we can obtain through the following method.

1. Get the system properties of JVM

 import java.util.Properties;
 public class ReadJVM {
  public static void main(String[] args) {
   Properties pps = System.getProperties();
   pps.list(System.out);
  }
 }

Result:


2 , Just create a new configuration file (Test.properties)

name=JJ
Weight=4444
Height=3333
 
 public class getProperties {
  public static void main(String[] args) throws FileNotFoundException, IOException {
   Properties pps = new Properties();
   pps.load(new FileInputStream("Test.properties"));
   Enumeration enum1 = pps.propertyNames();//得到配置文件的名字
   while(enum1.hasMoreElements()) {
    String strKey = (String) enum1.nextElement();
    String strValue = pps.getProperty(strKey);
    System.out.println(strKey + "=" + strValue);
   }
  }
 }

3. A more comprehensive example

Read value according to key

Read all information of properties

Write new properties information

 //关于Properties类常用的操作
 public class TestProperties {
  //根据Key读取Value
  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;
   }
  }
  
  //读取Properties的全部信息
  public static void GetAllProperties(String filePath) throws IOException {
   Properties pps = new Properties();
   InputStream in = new BufferedInputStream(new FileInputStream(filePath));
   pps.load(in);
   Enumeration en = pps.propertyNames(); //得到配置文件的名字
   
   while(en.hasMoreElements()) {
    String strKey = (String) en.nextElement();
    String strValue = pps.getProperty(strKey);
    System.out.println(strKey + "=" + strValue);
   }
   
  }
  
  //写入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);
   //调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。 
   //强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
   OutputStream out = new FileOutputStream(filePath);
   pps.setProperty(pKey, pValue);
   //以适合使用 load 方法加载到 Properties 表中的格式, 
   //将此 Properties 表中的属性列表(键和元素对)写入输出流 
   pps.store(out, "Update " + pKey + " name");
  }
  
  public static void main(String [] args) throws IOException{
   //String value = GetValueByKey("Test.properties", "name");
   //System.out.println(value);
   //GetAllProperties("Test.properties");
   WriteProperties("Test.properties","long", "212");
  }
 }

结果:

Test.properties中文件的数据为:

#Update long name
#Sun Feb 23 18:17:16 CST 2016
name=JJ
Weight=4444
long=212
Height=3333

The above is the detailed content of A brief introduction to Properties in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn