Java Read configuration file method

Source: Internet
Author: User

in the real work, we often need to save some system configuration information, we generally will choose to complete the configuration file, this article according to the author's work to read the configuration file of a small sum, the main description is spring read the configuration file method.

Reading an XML configuration file

(a) Create a new Java bean

Package chb.demo.vo;

public class Hellobean {

Private String HelloWorld;

Public String Gethelloworld () {

return helloWorld;

}

public void Sethelloworld (String helloWorld) {

This.helloworld = HelloWorld;

}

}

(ii) construct a configuration file

<?xml version= "1.0" encoding= "UTF-8"?>

<! DOCTYPE beans Public "-//spring//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd" >

<beans>

<bean id= "Hellobean" class= "Chb.demo.vo.HelloBean" >

<property name= "HelloWorld" >

<value>Hello!chb!</value>

</property>

</bean>

</beans>

(iii) Reading XML files

1. Using Classpathxmlapplicationcontext

ApplicationContext context = new Classpathxmlapplicationcontext ("Beanconfig.xml");

Hellobean Hellobean = (Hellobean) context.getbean ("Hellobean");

System.out.println (Hellobean.gethelloworld ());

2. Using Filesystemresource to read

Resource rs = new Filesystemresource ("D:/software/tomcat/webapps/springwebdemo/web-inf/classes/beanconfig.xml");

Beanfactory factory = new Xmlbeanfactory (RS);

Hellobean Hellobean = (Hellobean) factory.getbean ("Hellobean");

System.out.println (Hellobean.gethelloworld ());

It is worth noting that, with Filesystemresource, the configuration file must be placed in the project direct directory, or the absolute path should be specified, or the exception that cannot find the file will be thrown.

Second, read the properties configuration file

Here are two techniques: using spring to read properties files and using Java.util.Properties to read

(i) using spring to read properties files

We also use the Hellobean.java file above to construct the following beanconfig.properties file:

Hellobean.class=chb.demo.vo.hellobean

hellobean.helloworld=hello!chb!

The "Hellobean" name in the property file is the alias setting for the Bean. Class is used to specify the source of the class.

Then use Org.springframework.beans.factory.support.PropertiesBeanDefinitionReader to read the property file

Beandefinitionregistry reg = new Defaultlistablebeanfactory ();

Propertiesbeandefinitionreader reader = new Propertiesbeandefinitionreader (reg);

Reader.loadbeandefinitions (New Classpathresource ("Beanconfig.properties"));

Beanfactory factory = (beanfactory) reg;

Hellobean Hellobean = (Hellobean) factory.getbean ("Hellobean");

System.out.println (Hellobean.gethelloworld ());

(ii) using Java.util.Properties to read property files

For example, we construct a ipconfig.properties to hold the server IP address and port, such as:

ip=192.168.0.1

port=8080

, we can use the following procedure to obtain the server configuration information:

InputStream InputStream = This.getclass (). getClassLoader (). getResourceAsStream ("ipconfig.properties");

Properties P = new properties ();

try {

P.load (InputStream);

catch (IOException E1) {

E1.printstacktrace ();

}

System.out.println ("IP:" +p.getproperty ("IP") + ", Port:" +p.getproperty ("port"));

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.