在idea中使用java读取properties乱码的解决
来源:互联网 发布:qq for linux 2017 编辑:程序博客网 时间:2024/06/05 07:09
读取properties的方法是参考网友:五种方式让你在java中读取properties文件内容不再是难题
代码如下:
package com.ufgov.util;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.util.Properties;/** * Desc:properties文件获取工具类 * Created by lihhz on 2017/7/4. */public class PropertyUtil { private static final Logger logger = LoggerFactory.getLogger(PropertyUtil.class); private static Properties props; static{ loadProps(); } synchronized static private void loadProps(){ logger.info("开始加载properties文件内容......."); props = new Properties(); InputStream in = null; try { //第一种,通过类加载器进行获取properties文件流 in = PropertyUtil.class.getClassLoader().getResourceAsStream("conf/cpaf-message.properties"); //第二种,通过类进行获取properties文件流 //in = PropertyUtil.class.getResourceAsStream(""); props.load(in); } catch (FileNotFoundException e) { e.printStackTrace(); logger.error("cpaf-message.properties文件未找到"); } catch (IOException e) { logger.error("出现IOException"); e.printStackTrace(); } finally { try { if(null != in) { in.close(); } } catch (IOException e) { logger.error("cpaf-message.properties文件流关闭出现异常"); e.printStackTrace(); } } logger.info("加载properties文件内容完成..........."); logger.info("properties文件内容:" + props); } public static String getProperty(String key){ if(null == props) { loadProps(); } return props.getProperty(key); } public static String getProperty(String key, String defaultValue) { if(null == props) { loadProps(); } return props.getProperty(key, defaultValue); } public static void main(String[] args) throws Exception{ //ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); System.out.println(getProperty("cpaf_app_again_90"));//äºå¡æ设ç«ç³è¯·ï¼%sï¼éæ°æ¹åä¸éè¿ï¼è¯·åæ¶å°çè´¢æ¿é¨é¨é¢åç¸å ³æ件ã System.out.println(new String(getProperty("cpaf_app_again_90").getBytes("iso-8859-1"),"utf-8"));//请及时到省财政部门领取相关文件。 System.out.println("戴假发");//戴假发 }}
如上,在main方法中的乱码,但是使用ISO-8859-1,打印结果却是正常的。
好久没有遇到过乱码问题了,查了一些资料,找到一些有用的内容记录一下
1.idea中properties的编码设置
2.
in = PropertyUtil.class.getClassLoader().getResourceAsStream("conf/cpaf-message.properties");读取的是字节流,但是中文是无法使用字节流而必须使用字符流的,所以,props.load(in);改为props.load(new InputStreamReader(in));即可,读取出来的内容就正常了.
阅读全文
0 0
- 在idea中使用java读取properties乱码的解决
- Springboot解决IDEA读取properties配置文件的中文乱码
- 在eclipse中java读取properties文件时乱码
- Java读取properties文件中文乱码问题的解决
- 解决JAVA,properties loadFromXML读取中文XML乱码的方法
- 解决Java读取properties文件中文乱码的问题
- 解决使用Properties,读取中文乱码问题
- Java 读取properties文件中文乱码解决
- Springboot 之 解决IDEA读取properties配置文件的中文乱码问题
- Springboot 之 解决IDEA读取properties配置文件的中文乱码问题
- Springboot 之 解决IDEA读取properties配置文件的中文乱码问题
- Springboot 之 解决IntelliJ IDEA读取properties配置文件的中文乱码问题
- Springboot 之 解决IDEA读取properties配置文件的中文乱码问题
- Springboot 之 解决IDEA读取properties配置文件的中文乱码问题
- Springboot 之 解决IDEA读取properties配置文件的中文乱码问题
- 解决在idea中使用maven插件时出现乱码
- Intellij IDEA 中 .properties文件中文显示乱码问题的解决
- 解决Git Log在IDEA中乱码的问题
- vue项目构建步骤
- 《Advanced Bash-scripting Guide》学习(十二):占位符":"及其他
- vim的配置文件vimrc让vim强大起来
- 抽象代数学习笔记(4)置换
- linux 内核编译错误
- 在idea中使用java读取properties乱码的解决
- Hadoop配置使用Solr
- 50道Java线程面试题
- Drools的安装
- 那些考上大学的孩子,最后都怎么样了?
- 在 Linux 下用户空间与内核空间数据交换的方式,第 1 部分: 内核启动参数、模块参数与sysfs、sysctl、系统调用和netlink
- mysql状态监控
- MultiModel:跨领域多任务机器学习
- python sys.path.append