How Java loads a configuration file

Source: Internet
Author: User

One uses native mode to read the configuration file 1 file system load Java code  inputstream in = new FileInputStream ("Config.properties");        properties p = new Properties ();  p.load (in);  2 class Load mode A and class sibling directory Java code  inputstream in = Main.class.getResourceAsStream ("config.properties");  b in the next level directory of the class Java code  inputstream in =   main.class.getresourceasstream ("resource/ Config.properties "); &NBSP;C Specifies the classes relative path of the load resource profile java code  inputstream in =   main.class.getresourceasstream ("/test/ Resource/config.properties ");   NOTE: As On/start, the load starts from the root directory. D the way the class loader is used Java code  inputstream in = Main.class.getClassLoader ().  getresourceasstream ("Test/resource/config.properties");  e resource configuration file Under Classes Java code  inputstream in =   main.class.getclassloader (). getResourceAsStream (" Config.properties ");   II Use the functional components provided by Apache to refer to the relevant information on the Web 1 sample Code Java code  import java.util.List for reading the properties file;  import org.apache.commons.configuration.Configuration;  import org.Apache.commons.configuration.ConfigurationException;  import org.apache.commons.configuration.PropertiesConfiguration;    public class Propertiesconfigurationdemo {       public static void main ( String[] args) throws ConfigurationException {           Configuration config = new Pr Opertiesconfiguration (PropertiesConfigurationDemo.class.getResource ("system-config.properties"));          String IP = config.getstring ("IP");                 string[] colors = Config.getstringarray ("Colors.pie");           for (int i = 0; i < colors.length; i++) {         &NB Sp   SYSTEM.OUT.PRINTLN (colors[i]);         }                       &NBS P list<?> colorlist = config.getlist ("Colors.pie");  &nBsp       for (int i = 0; i < colorlist.size (); i++) {             Syste M.out.println (Colorlist.get (i));         }       }  }   configuration file code: Ip=10.10.1.1colors.pie = #FF0000, #00 FF00, #0000FF2 the way the XML file is read Java code  import java.util.List;  import org.apache.commons.configuration.Configuration;  import org.apache.commons.configuration.ConfigurationException;  import org.apache.commons.configuration.XMLConfiguration;    public class Xmlconfigurationdemo {       public static void main (string[] args) throws ConfigurationException {           Configuration config = new Xmlconfigur ation (xmlconfigurationdemo.class                  GetResource (" System-config.xml "));          String IP = config.getstring ("IP");         String account = config.getstring ("account");          String password = config.getstring ("password");          list<?> roles = Config.getlist ("Roles.role");          System.out.println ("IP:" + IP);          SYSTEM.OUT.PRINTLN ("account:" + account);          SYSTEM.OUT.PRINTLN ("Password:" + Password);                  for (int i = 0; i < roles.size (); i++) {   &NB Sp         SYSTEM.OUT.PRINTLN ("Role:" + roles.get (i));         }            }  }  xml file code: XML code &NBSP;&LT ;config>      <ip>10.10.1.1</ip>      <account>jini</account >      <password>jakarta99</password>      <roles>          <role>admin</role>          <role>manager</role>          <role>user</role>      </roles>  </config >   Example 2Java code  import org.apache.commons.configuration.Configuration;  import org.apache.commons.configuration.ConfigurationException;  import org.apache.commons.configuration.XMLConfiguration;  public class Xmlconfigurationexample {     public static void main (string[] args) throws Exception,              ConfigurationException {         String Resource = "Main/global.xml";          Configuration config = new xmlconfiguration (Resource);         //Only a new instance of Xmlconfiguration is available.          list<?> Startcriteria = config.getlist ("Start-criteria.criteria");     &NBSp            for (int i = 0; i < startcriteria.size (); i++) {       &N Bsp     SYSTEM.OUT.PRINTLN ("Role:" + startcriteria.get (i));         }          int horsepower = Config.getint ("horsepower");          System.out.println (horsepower);     }    global.xml file code XML code  <?xml version= "1.0" encoding= "iso-8859-1"?>   <engine-config>      <start-criteria>          <criteria> & nbsp;            temperature Above-10 Celsius           </ Criteria>          <criteria>              Fuel tank is not empty          </criteria>      </start-criteria>       &LT;VOLume>4 liters</volume>     

How Java loads a configuration file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.