Home>

1 Introduction:

The java.util.properties class provided by jdk inherits from the hashtable class and implements the map interface. It uses a form of key-value pairs to save property sets.Where keys and values ​​are both string types.

The java.util.properties class provides getproperty () and setproperty () methods to manipulate property files.Use the load () method and store () method to load and save the properties configuration file at the same time.

The java.util.resourcebundle class also provides methods for reading properties configuration files.resourcebundle is an abstract class.

Main methods in 2.properties

1) load (inputstream instream):This method can be downloaded from.The number of files corresponding to the properties file is entered into the stream.Load the property list into the properties class object.Load has two method overloads:load (inputstream instream) and load (reader reader), which can load property files in different ways.

inputstream instream=testproperties.class.getclassloader (). getresourceasstream ("demo.properties");
//Get via the getresourceasstream method of the current class loader
//testproperties current class name;
testproperties.class. Get the class object to which the current object belongs;
inputstream in=classloader.getsystemresourceasstream ("filepath");
inputstream instream=new fileinputstream (new file ("filepath"));//Get from file
inputstream in=context.getresourceasstream ("filepath");//In the servlet, the inputstream can be obtained through the context
inputstream instream=new url ("path"). openstream ();//Get by url

The reading method is as follows:

properties pro=new properties ();//instantiate a properties object
inputstream instream=new fileinputstream ("demo.properties");//Get the file input stream of the properties file
pro.load (nstream);
instream.close ();

2) store (outputstream out, string comments):This method writes the property list of the properties class object.properties configuration file.as follows:

fileoutputstream outstream=new fileoutputstream ("demo.properties");
pro.store (outstream, "comment");
outstream.close ();

3 main methods in resourcebundle

Get it through the resourcebundle.getbundle () static method,This method does not need to add to get the properties file.Properties suffix. You can also get a resourcebundle object from the inputstream.

resourcebundle resource=resourcebundle.getbundle ("com/xiang/demo");//emo is the property file name,Put it under the package com.xiang, if it is put under src, just use test
resourcebundle resource1=new propertyresourcebundle (instream);
string value=resource.getstring ("name");

The problem encountered in use may be the path of the configuration file,When the configuration file is not in the same package as the current class,You need to use package name qualification;If the properties file is in the src root directory,Just use demo.properties or demo.

4 properties operation example

import java.io.file;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.util.hashmap;
import java.util.iterator;
import java.util.map;
import java.util.properties;
/**
 * java preperties configuration file tool class
 * @author shu
 *
 * /
public class propsutil {
  private string path="";
  private properties properties;
  /**
   * Default constructor
   * /
  public propsutil () {}
  /**
   * Constructor
   * @param path Pass in the property address value
   * /
  public propsutil (string path) {
    this.path=path;
  }
  /**
   * Load properties file
   * @return returns the read properties object
   * /
  public properties loadprops () {
    inputstream instream=classloader.getsystemresourceasstream (path);
    try {
      if (instream == null)
        throw new filenotfoundexception (path + "file is not found");
      properties=new properties ();
      properties.load (instream);
      instream.close ();
    } catch (ioexception e) {
      e.printstacktrace ();
    }
    return properties;
  }
  /**
   * Write configuration to file
   * /
  public void writefile () {
    //Get file output stream
    try {
      fileoutputstream outputstream=new fileoutputstream (new file (classloader.getsystemresource (path) .touri ()));
      properties.store (outputstream, null);
      outputstream.close ();
    } catch (exception e) {
      //todo auto-generated catch block
      e.printstacktrace ();
    }
  }
  /**
   * Get value by keyword
   * @param key
   * @return returns the corresponding string,If none, return null
   * /
  public string getvaluebykey (string key) {
    if (properties == null)
      properties=loadprops ();
    string val=properties.getproperty (key.trim ());
    return val;
  }
  /**
   * Get value by keyword
   * @param key keyword to get
   * @param defaultvalue The value returned if the corresponding keyword cannot be found
   * @return returns the found string
   * /
  public string getvaluebykey (string key, string defaultvalue) {
    if (properties == null)
      properties=loadprops ();
    return properties.getproperty (key, defaultvalue);
  }
  /**
   * Get all values ​​of properties
   * @return returns key-value pairs of properties
   * /
  public map<string, string>getallproperties () {
    if (properties == null)
      properties=loadprops ();
    map<string, string>map=new hashmap<string, string>();
    //get all the key values
    iterator<string>it=properties.stringpropertynames (). iterator ();
    while (it.hasnext ()) {
      string key=it.next ();
      map.put (key, properties.getproperty (key));
    }
    /* enumeration enumeration=properties.propertynames ();
    while (enumeration.hasmoreelements ()) {
      string key=(string) enumeration.nextelement ();
      string value=getvaluebykey (key);
      map.put (key, value);
    } * /
    return map;
  }
  /**
   * Write new key value to properties and save
   * @param key corresponding key
   * @param value corresponding value
   * /
  public void addproperties (string key, string value) {
    if (properties == null)
      properties=loadprops ();
    properties.setproperty (key, value);
    try {
      writefile ();
    } catch (exception e) {
      throw new runtimeexception ("write fail");
    }
  }
  /**
   * Update configuration file
   * @param key corresponding key
   * @param value corresponding value
   * /
   public void update (string key, string value) {
     if (properties == null)
      properties=loadprops ();
     if (properties.containskey (key))
       properties.replace (key, value);
    try {
      writefile ();
    } catch (exception e) {
      throw new runtimeexception ("write fail");
    }
   }
   /**
   * Delete a key-value pair
   * @param key
   * /
   public void deletebykey (string key) {
     if (properties == null)
      properties=loadprops ();
     if (! properties.containskey (key))
       throw new runtimeexception ("not such key");
     properties.remove (key);
     try {
      writefile ();
     } catch (exception e) {
      throw new runtimeexception ("write fail");
    }
   }
   /**
   * Set the path value
   * @param path
   * /
   public void setpath (string path) {
     this.path=path;
   }
}
  • Previous Tween algorithm implemented in JavaScript and buffer effect example code
  • Next C # method of operating the registry