Java reads the configuration file, java configuration file

Source: Internet
Author: User

Java reads the configuration file, java configuration file

Java can use Word-based throttling and merge stream to read files. Because a Chinese character occupies two bytes, garbled characters may occur if the configuration file contains Chinese characters and is read with throttling. No garbled characters will appear when you use the RST stream.

The configuration file B. properties is as follows:

Family \ name = zhou
Second name = dingzhao
Gender = male
Tel no. = + 86 (-) 13913462
Salary = 0.001 k
Occupation = java & Engineer

 

The code for reading the configuration file is as follows:

Properties pro = new Properties ();

Try {

FileReader in2 = new FileReader (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.001 k
Occupation: java & Engineer

Java & Engineers

Note:

1. If the configuration file contains spaces, use the \ escape character.

2. The getProperty ("Occupation") getProperty (key) method can obtain the variable value in the configuration file.

 

The byte stream code is as follows:

Properties pro = new Properties ();

Try {

InputStream in2 = new BufferedInputStream (new FileInputStream ("I: \ Workspaces \ MyWork \ xcserver \ src \ com \ xiaocong \ user \ service \. 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 ();

}

The Chinese characters in the byte stream configuration file are garbled.

  

 

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.