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.