Java读取properties文件工具类并解决控制台中文乱码
1、建立properts文件(error.message.properties)
HTTP201= 请求成功并且服务器创建了新的资源

2、在spring-mvc.xml文件(applicationContext-mvc.xml)中配置properties工具类路径及读取properties文件的路径
<bean id="propertyConfigurer" class="com.yjlc.platform.utils.PropertyConfigurer" >
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="ignoreResourceNotFound" value="true"/>
<property name="locations">
<list>
<value>classpath:error.message.properties</value>
</list>
</property>
<property name="fileEncoding" value="UTF-8"></property>
</bean>

3、建立properts读取工具类
package com.yjlc.platform.utils; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import java.util.Properties; /**
* --------------------------------------------------------------
* CopyRights(c)2018,YJLC
* All Rights Reserved
* <p>
* FileName: PropertyConfigurer.java
* Description:错误信息文件读取工具类
* Author: cyb
* CreateDate: 2019-01-18
* --------------------------------------------------------------
*/
public class PropertyConfigurer extends PropertyPlaceholderConfigurer {
private Properties props; // 存取properties配置文件key-value结果 @Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
throws BeansException {
super.processProperties(beanFactoryToProcess, props);
this.props = props; }
//输入配置文件中的key 获取对应的值
public String getProperty(String key){
return new String(props.getProperty(key)); } public String getProperty(String key, String defaultValue) {
return this.props.getProperty(key, defaultValue);
} public Object setProperty(String key, String value) {
return this.props.setProperty(key, value);
}
}
4、建立对应的service及实现类
(1) Service接口(GetMessageInfoService )
package com.yjlc.platform.upgrade.common.service; import java.io.UnsupportedEncodingException; /**
* --------------------------------------------------------------
* CopyRights(c)2018,YJLC
* All Rights Reserved
* <p>
* FileName: GetMessageInfoService.java
* Description:获取错误信息service
* Author: cyb
* CreateDate: 2019-01-18
* --------------------------------------------------------------
*/
public interface GetMessageInfoService { /**
* 第四种实现方式获取properties文件中指定key的value
*
* @param key
*
* @return
*/
String getProperyByFourthWay(String key) throws UnsupportedEncodingException; /**
* 第四种实现方式获取properties文件中指定key的value
*
* @param key
*
* @param defaultValue
*
* @return
*/
String getProperyByFourthWay(String key, String defaultValue); }
Service实现类
package com.yjlc.platform.upgrade.common.service.impl; import com.yjlc.platform.upgrade.common.service.GetMessageInfoService;
import com.yjlc.platform.utils.PropertyConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; /**
* --------------------------------------------------------------
* CopyRights(c)2018,YJLC
* All Rights Reserved
* <p>
* FileName: PropertiesServiceImpl.java
* Description:获取错误信息service实现类
* Author: cyb
* CreateDate: 2019-01-18
* --------------------------------------------------------------
*/
@Service
public class GetMessageInfoServiceImpl implements GetMessageInfoService { @Autowired
private PropertyConfigurer pc; @Override
public String getProperyByFourthWay(String key) {
return pc.getProperty(key);
} @Override
public String getProperyByFourthWay(String key, String defaultValue) {
return pc.getProperty(key, defaultValue);
} }
5、调用测试
@Controller
@RequestMapping("/upInformation/")
public class UPInformationController {
@Autowired
GetMessageInfoService getMessageInfoService; @RequestMapping("forwardInfo")
@ResponseBody
public Map<String,Object> forwardInfo(UPInformationComment informationComment,String status) throws UnsupportedEncodingException {
Map<String,Object> map = new HashMap<>(); String value= getMessageInfoService.getProperyByFourthWay("HTTP201"); } }
6、控制台打印中文乱码问题解决
找到intellij idea安装目录,bin文件夹下面idea64.exe.vmoptions和idea.exe.vmoptions这两个文件,分别在这两个文件中添加:-Dfile.encoding=UTF-8
第二步:找到intellij idea的file---settings---Editor---FileEncodings的GlobalEncoding和ProjectEncoding和Default encoding for properties都配置成UTF-8
第三步:在部署Tomcat的VM options项中添加:-Dfile.encoding=UTF-8

第四步:重启Intellij idea即可解决乱码问题(一定要关闭idea重新打开)
此篇博客,本人参照了以下博主的内容,再根据自己的需求进行了整合,若需要查看详细内容,大家可以前往以下链接查看:https://www.cnblogs.com/hafiz/p/5876243.html#commentform
技术在于交流!
Java读取properties文件工具类并解决控制台中文乱码的更多相关文章
- Java读取properties配置文件工具类
1. PropertyUtils.java package javax.utils; import java.io.InputStream; import java.util.Properties ...
- 读取Properties文件工具类
import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java ...
- java读取properties的工具类PropertiesUtil
package org.properties.util; import java.io.FileInputStream; import java.io.FileOutputStream; import ...
- java读取properties文件工具
public class PropertiesUtil { public static String get(String filePath, String key) { String val = n ...
- java读取.txt文件工具类FileUtiles
public class FileUtils { private static final String ENCODING = "UTF-8";//编码方式 /** * 获取文件的 ...
- java实现ftp文件上传下载,解决慢,中文乱码,多个文件下载等问题
//文件上传 public static boolean uploadToFTP(String url,int port,String username,String password,String ...
- 读取Config文件工具类 PropertiesConfig.java
package com.util; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io ...
- java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)
java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...
- 用java读取properties文件--转
今天为了通过java读取properties文件,google了很长时间,终于找到了.现在特记录之和大家一起分享. 下面直接贴出代码:java类 public class Mytest pub ...
随机推荐
- MongoDB调优-查询优化-MongoDB Profiler
MongoDB查询优化-MongoDB Profiler MongoDB Profiler 概述 官方文档:https://docs.mongodb.com/manual/tutorial/manag ...
- PM2来部署nodejs服务器永久开启
pm2 日常使用 1. pm2 是什么? 日常开发中需要启动一个node项目,需要用npm run …,,如果终端被关掉,程序也就自动停止,有时候几个项目一起跑起来,好几个终端开着,个人不太喜欢, ...
- ElasticSearch聚合分析
聚合用于分析查询结果集的统计指标,我们以观看日志分析为例,介绍各种常用的ElasticSearch聚合操作. 目录: 查询用户观看视频数和观看时长 聚合分页器 查询视频uv 单个视频uv 批量查询视频 ...
- 判断使用设备是PC还是phone
<script type="text/javascript"> //如果是手机设备,则.. if(/Android|webOS|iPhone|iPod|BlackBer ...
- win10 关闭自动更新
方法一 : 利用组策略关闭win10自动更新的步骤如下:1.按win+R打开“运行”,输入“gpedit.msc”,按下回车. 2.找到“计算机配置”→““管理模板”→“Windows 组件”→“Wi ...
- [android] 图片的缩放
界面布局,线性布局,竖直排列,两个ImageView 获取到两个ImageView对象 调用BitmapFactory.decodeResource(res,id)方法,获取Bitmap对象 参数:r ...
- Python 获取时间
记录下 Python 下获取时间的方法 time 模块 import time time_format = '%Y-%m-%d %X' time_current = time.strftime(tim ...
- 2017-11-20 中文代码示例之Vuejs入门教程(一)问题后续
"中文编程"知乎专栏原文 第一个issue: Error compiling template if using unicode naming as v-for alias · I ...
- 【代码笔记】Web-HTML-框架
一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- python小练习:用户三次登陆, 购物车
2018.12.1 周末练习: 1.用户三次登陆 from random import randint i = 1 while i < 4: num = 0 verify_code = '' w ...