sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  1795 随笔 :: 22 文章 :: 24 评论 :: 226万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

java 读取properties java读取properties乱码,IDEA 更改显示格式


1. 打开properties文件,中文呈现乱码:

原因:文件格式问题,properties默认使用ISO8859-1格式,中文显示,通用的是utf-8, 带中文的可改成gbk, gb2312. 这里改成         utf-8即可。

步骤:选中文件,右键-->properties-->resource,"text file coding",默认是选择的defalut:"ISO8895-1",修改成"other",下拉       选择utf-8即可。

2.读取properties文件时出现乱码:eg: String username=property.getProperty(key);

原因:getProperty(key)是使用ISO8859-1来解码的,所以读取中文时会乱码,需要将读取出来的字符串从ISO8859-1再编码回         去,用文本的本身编码格式再解码。

解决方法:

       

在 String username=property.getProperty(key); 后增加:
         resultName=new String(username.getBytes("ISO-8859-1"),"gbk");
  • 1.
  • 2.

直接使用resultName就行。

3. (1)在Properties.load中:

    eg: java代码:

FileInputStream isr = new FileInputStream(savePath); 
         Properties props = new Properties(); 
         props.load(isr);
  • 1.
  • 2.
  • 3.

注意:isr 必须是文件字节流,不能是字符流,否则还是会乱码。(或者有解决办法,但是我还没找到!)

(2) 输出流:

eg: Java代码 

OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(savePath), "UTF-8"); 
 props.store(osw,"This is the System Config file,Please don't delete or modify it!");
  • 1.
  • 2.
原文链接:https://blog.51cto.com/u_16099220/6336759
posted on   sunny123456  阅读(315)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示