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 ...
随机推荐
- AIOps 在腾讯的探索和实践
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由LemonLu发表于云+社区专栏 赵建春 腾讯 技术运营通道主席 腾讯 社交网络运营部助理总经理 AIOps 白皮书核心编写专家 我今 ...
- spring boot 2.0 源码分析(一)
在学习spring boot 2.0源码之前,我们先利用spring initializr快速地创建一个基本的简单的示例: 1.先从创建示例中的main函数开始读起: package com.exam ...
- PHP 九九乘法表的4种表达方式
九九乘法表的四种不同表现形式 x轴对称: //第一种 for($i=1;$i<=9;$i++){ for($j=1;$j<=$i;$j++) { echo $i.'x'.$j.'='.$i ...
- /proc文件系统 - 汇总
0. /proc目录简介 Linux 内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制. proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间 ...
- Spring Boot 初始化运行特定方法
Spring Boot提供了两种 “开机自启动” 的方式,ApplicationRunner和CommandLineRunner 这两种方式的目的是为了满足,在容器启动时like执行某些方法.我们可以 ...
- 菜鸟入门【ASP.NET Core】5:命令行配置、Json文件配置、Bind读取配置到C#实例、在Core Mvc中使用Options
命令行配置 我们通过vs2017创建一个控制台项目CommandLineSample 可以看到现在项目以来的是dotnet core framework 我们需要吧asp.net core引用进来 ...
- PowerDesigner 参照完整性约束(转载)
PowerDesigner 参照完整性约束: 限制(Restrict):不允许进行修改或删除操作.若修改或删除主表的主键时,如果子表中存在子记录,系统将产生一个错误提示.这是缺省的参照完整性设置. 置 ...
- Spark中master与worker的进程RPC通信实现
1.构建master的actor package SparkRPC import akka.actor.{Actor, ActorSystem, Props}import com.typesafe.c ...
- [PHP] 算法-构建排除当前元素的乘积数组的PHP实现
构建乘积数组给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]. ...
- 【Java深入研究】7、ThreadLocal详解
ThreadLocal翻译成中文比较准确的叫法应该是:线程局部变量. 这个玩意有什么用处,或者说为什么要有这么一个东东?先解释一下,在并发编程的时候,成员变量如果不做任何处理其实是线程不安全的,各个线 ...