Article Directory
foreword
When using properties for the first time, if the value is in Chinese when reading the java properties file, there will be 乱码
problems.
1. country.properties file
Create a sk.properties property file under the project's default path (src directory) (the name can be customized, and the extension must be properties).
120000=天津市
130100=石家庄市
2. Write and read the properties attribute file, and output the attribute value.
//从配置文件获取密钥信息
private static final Properties properties = getProperties();
private static String getProperties(){
Properties properties = new Properties();
try {
//生产环境根目录
FileInputStream inputStream = new FileInputStream("src/main/resources/city.properties"); //TODO 本地测试打开
properties.load(inputStream); //TODO 本地测试打开
} catch (IOException e) {
e.printStackTrace();
}
return properties.getProperty("vehLocationPartitions");
}
public void test(){
String name=properties.getProperty("120000");
// 输出结果
System.out.println("name: "+name);
}
output result
天津市
After the above program is executed, Chinese garbled characters will appear in the result, because the byte stream cannot read Chinese, so the reader is used to convert the inputStream into a reader
character stream to read Chinese.
The modified code is as follows
//从配置文件获取密钥信息
private static final Properties properties = getProperties();
private static String getProperties(){
Properties properties = new Properties();
try {
//生产环境根目录
FileInputStream inputStream = new FileInputStream("src/main/resources/city.properties"); //TODO 本地测试打开
properties.load(new InputStreamReader(inputStream)); //TODO 本地测试打开
} catch (IOException e) {
e.printStackTrace();
}
return properties.getProperty("vehLocationPartitions");
}
public void test(){
String name=properties.getProperty("120000");
// 输出结果
System.out.println("name: "+name);
}
The modified run result
Summarize
If this article is helpful to you, I hope that the big guys can
关注
,点赞
,收藏
,评论
support a wave, thank you very much!
Please correct me if I am wrong!!!