java 读取properties配置文件内容乱码 --日文乱码对应方法
生活随笔
收集整理的這篇文章主要介紹了
java 读取properties配置文件内容乱码 --日文乱码对应方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一 環境配置:Win7+Eclipse3.5+JDK1.5?
二 示例代碼:?
1 主文件代碼?
Java代碼?? import?java.io.FileInputStream;?? import?java.io.IOException;?? import?java.io.InputStream;?? import?java.util.Properties;?? ?? public?class?Main?{?? ?????? ????public?static?void?main(String[]?args)?throws?Exception{?? ????????readConfigFile();?? ????}?? ?? ????private?static?void?readConfigFile()?throws?IOException?{????????? ????????InputStream?in?=?new?FileInputStream("config.properties");???? ????????Properties?p?=?new?Properties();?? ????????p.load(in);?? ????????String?username?=?p.getProperty("username");?? ????????String?sex?=?p.getProperty("sex");?? ????????String?age?=?p.getProperty("age");?? ????????System.out.println("姓名:"+username+",性別:"+sex+",年齡:?"+age);?/*p.setProperty("username",?"陳文");? ????????p.setProperty("sex",?"男");? ????????p.setProperty("age",?"44");?????? ????????username?=?p.getProperty("username");? ????????sex?=?p.getProperty("sex");? ????????age?=?p.getProperty("age");? ????????System.out.println("姓名:"+username+",性別:"+sex+",年齡:?"+age);??*/?? ?????????? ?????????? ????}?? }??
2 配置文件內容:?
Java代碼?? username=陳放?? sex=女?? age=23??
三 調試?
將中文的配置文件編碼設置為UTF-8,GBK格式,都不行,讀取配置文件內容都是亂碼,?
不是中文的都正常。?
解決方式一:?
經過調試:配置文件的編碼格式必設置為:GBK,在代碼中在進行轉換操作就行了。?
String username =?
new String(p.getProperty("username").getBytes("ISO8859-1"),"GBK");?
解決方式二:?
對配置文件進行GBK編碼,正常讀取就行了,不需要進行字符集的轉換:?
下面是編碼小工具:?
D:?
cd D:/bea/jdk150_04/bin?
native2ascii -encoding? GBK? F:/resurl.properties? F:/resurl_zh.properties ?
二 示例代碼:?
1 主文件代碼?
Java代碼??
2 配置文件內容:?
Java代碼??
三 調試?
將中文的配置文件編碼設置為UTF-8,GBK格式,都不行,讀取配置文件內容都是亂碼,?
不是中文的都正常。?
解決方式一:?
經過調試:配置文件的編碼格式必設置為:GBK,在代碼中在進行轉換操作就行了。?
String username =?
new String(p.getProperty("username").getBytes("ISO8859-1"),"GBK");?
解決方式二:?
對配置文件進行GBK編碼,正常讀取就行了,不需要進行字符集的轉換:?
下面是編碼小工具:?
D:?
cd D:/bea/jdk150_04/bin?
native2ascii -encoding? GBK? F:/resurl.properties? F:/resurl_zh.properties ?
將以上保存為.bat文件格式就行了。這樣可解決很多問題,可修改文件中的值(如以上注釋了的部分),在進行讀取也是正常的,如用第一種方式讀取就是亂碼。?
以上是轉載文章
日文環境下,配置文件格式保持為SJIS或SHIFT_JIS(文本軟件不同名字不同),然后
String username =?
new String(p.getProperty("username").getBytes("ISO8859-1"),"SHIFT_JIS");?
或
cd D:/bea/jdk150_04/bin?
native2ascii -encoding? SHIFT_JIS ?F:/resurl.properties? F:/resurl_jp.properties?
最簡單的方法是,下載eclipse的插件 properties editor,進行文本轉換。
總結
以上是生活随笔為你收集整理的java 读取properties配置文件内容乱码 --日文乱码对应方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: golang log日志
- 下一篇: 搜索文件夹中word文档中的关键字