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