Java Read configuration file

Source: Internet
Author: User

Java read files can be used with byte stream and character stream. Since a Chinese character accounts for two bytes, if there is a Chinese character in the configuration file, it will be garbled if it is read with a byte stream. Character streams are not garbled.

The configuration file B.properties file is as follows:

family\ name = Zhou
Second name = Dingzhao
Gender = Male
Tel No. = +86 (-) 13913462
Salary = 0.001k
Professional =java & Engineer

The code to read the configuration file is as follows:

Properties Pro = new properties ();

try {

FileReader in2 = new FileReader (The new File ("i:\\workspaces\\mywork\\xcserver\\src\\com\\xiaocong\\user\\service\\ B.properties "));

Pro.load (in2);

System.out.println (Pro.getproperty ("family name"));

Iterator<string> it=pro.stringpropertynames (). Iterator ();
while (It.hasnext ()) {
String Key=it.next ();
System.out.println (key+ ":" +pro.getproperty (key));
}

In2.close ();

} catch (FileNotFoundException e) {

E.printstacktrace ();

}

System.out.println (Pro.getproperty ("occupation"));

Output:

Zhou
Second:name = Dingzhao
Tel:no. = +86 (-) 139133462
Family Name:zhou
Gender:male
salary:0.001k
Occupation: Java & Engineer

Java & Engineers

Note:

1, there are spaces in the configuration file, you need to use the \ Escape character.

2, Pro.getproperty ("occupation") GetProperty (key) method can get the value of the variable in the configuration file.

  

Java Read configuration file

Related Article

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.