Java讀取properties文件之中文亂碼問(wèn)題及解決
Java讀取properties文件中文亂碼
初用properties,讀取java properties文件的時(shí)候如果value是中文,會(huì)出現(xiàn)讀取亂碼的問(wèn)題。
給定country.properties文件如下:
China=中國(guó) USA=美國(guó) Japan=日本
Properties properties = new Properties(); ? InputStream inputStream = this.getClass().getResourceAsStream("/country.properties"); ? properties.load(inputStream ); ? System.out.println(properties.getProperty("China")); ??
上面的程序執(zhí)行后的結(jié)果會(huì)出現(xiàn)中文亂碼,因?yàn)樽止?jié)流是無(wú)法讀取中文的,所以采取reader把inputStream轉(zhuǎn)換成reader用字符流來(lái)讀取中文。
代碼如下:
Properties properties = new Properties(); ? InputStream inputStream = this.getClass().getResourceAsStream("/country.properties"); ? BufferedReader bf = new BufferedReader(new ?InputStreamReader(inputStream)); ? properties.load(bf); ? System.out.println(properties.getProperty("China"));
兩種方式讀取properties配置文件
在Java中我們經(jīng)常會(huì)將我們自定義的配置文件xxx.properties,讀取到我們的Java代碼中去?,F(xiàn)在我目前已知有兩種讀取配置文件的方式,如下所示。
方式一:使用Properties集合工具類讀取配置文件。
Properties的加載方法
方法名 | 說(shuō)明 |
void load(Reader reader) | 從輸入字符流讀取屬性列表(鍵和元素對(duì)) |
void store(Writer writer, String comments) | 將此屬性列表(鍵和元素對(duì))寫(xiě)入此Properties表中,一適合使用load(Reader)方法的格式寫(xiě)入輸出字符流 |
加載完成后根據(jù)下面方法獲取值
方法名 | 說(shuō)明 |
Object setProperty(String key,String value) | 設(shè)置集合的鍵和值,都是String類型,底層調(diào)用 Hashtable方法put |
String getProperty(String key) | 使用此屬性列表中指定的鍵搜索屬性 |
Set<String> stringPropertyNames() | 從該屬性列表中返回一個(gè)不可修改的鍵集,其中鍵及其對(duì)應(yīng)的值是字符串。 |
代碼演示:
// properties文件略 Properties pro = new Properties(); int maxTotal = 0; int maxIdel = 0; String host = null; int port = 0; try { pro.load(new FileReader("D:\\360驅(qū)動(dòng)大師目錄\\Redis\\Jedis_Test\\src\\redis.properties")); maxTotal = Integer.parseInt(pro.getProperty("redis.maxTotal")); maxIdel = Integer.parseInt(pro.getProperty("redis.maxIdel")); host = pro.getProperty("redis.host"); port = Integer.parseInt(pro.getProperty("redis.port")); } catch (IOException e) { e.printStackTrace(); }
方式二:使用ResourceBundle工具類讀取配置文件
ResourceBoundle加載方法
返回類型 | 方法名 | 描述 |
static ResourceBundle | getBundle(String basename) | 使用指定的基本名稱,默認(rèn)語(yǔ)言環(huán)境和調(diào)用者的類加載器獲取資源包 |
加載完成后根據(jù)下面方法獲取值
返回類型 | 方法名 | 描述 |
Object | getObject(String key) | 從此資源包根據(jù)鍵獲取值,將值以O(shè)bject類型返回 |
String | getString(String key) | 從此資源包根據(jù)鍵獲取值,將值以String類型返回 |
String[] | getStringArray(String key) | 從此資源包根據(jù)鍵獲取值,將值以列表類型返回 |
代碼演示:
ResourceBundle bundle = ResourceBundle.getBundle("redis"); int maxTotal = Integer.parseInt(bundle.getString("redis.maxTotal")); int maxIdel = Integer.parseInt(bundle.getString("redis.maxIdel")); String host = bundle.getString("redis.host"); int port = Integer.parseInt(bundle.getString("redis.port"));
Java讀寫(xiě)資源文件類properties
Java中讀寫(xiě)資源文件最重要的類是Properties
1.資源文件要求如下
- properties文件是一個(gè)文本文件
- properties文件的語(yǔ)法有兩種,一種是注釋,一種屬性配置。
注 釋:前面加上#號(hào)
屬性配置:以“鍵=值”的方式書(shū)寫(xiě)一個(gè)屬性的配置信息。
- properties文件的一個(gè)屬性配置信息值可以換行,但鍵不可以換行。值換行用“\”表示。
- properties的屬性配置鍵值前后的空格在解析時(shí)候會(huì)被忽略。
- properties文件可以只有鍵而沒(méi)有值。也可以僅有鍵和等號(hào)而沒(méi)有值,但無(wú)論如何一個(gè)屬性配置不能沒(méi)有鍵。
eg:
正確的資源文件格式為:
2.功能大致如下
- 讀寫(xiě)Properties文件
- 讀寫(xiě)XML文件
- 不僅可以讀寫(xiě)上述兩類文件,還可以讀寫(xiě)其它格式文件如txt等,只要符合key=value格式即可.
Properties能讀取以key,value存儲(chǔ)的任何格式文件,看一下他的類結(jié)構(gòu)就知道為什么了
從上面的類結(jié)構(gòu)圖可以看出,它繼承了Hashtable并實(shí)現(xiàn)了Map接口
3.代碼演示
package com.itbird.myhome.test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.util.Properties; public class PropertiesMyTest { public static void main(String[] args) { String readfile = "e:" + File.separator + "readfile.properties"; String writefile = "e:" + File.separator + "writefile.properties"; String readxmlfile = "e:" + File.separator + "readxmlfile.xml"; String writexmlfile = "e:" + File.separator + "writexmlfile.xml"; String readtxtfile = "e:" + File.separator + "readtxtfile.txt"; String writetxtfile = "e:" + File.separator + "writetxtfile.txt"; readPropertiesFile(readfile); //讀取properties文件 writePropertiesFile(writefile); //寫(xiě)properties文件 readPropertiesFileFromXML(readxmlfile); //讀取XML文件 writePropertiesFileToXML(writexmlfile); //寫(xiě)XML文件 readPropertiesFile(readtxtfile); //讀取txt文件 writePropertiesFile(writetxtfile); //寫(xiě)txt文件 } //讀取資源文件,并處理中文亂碼 public static void readPropertiesFile(String filename) { Properties properties = new Properties(); try { InputStream inputStream = new FileInputStream(filename); properties.load(inputStream); inputStream.close(); //關(guān)閉流 } catch (IOException e) { e.printStackTrace(); } String username = properties.getProperty("username"); String passsword = properties.getProperty("password"); String chinese = properties.getProperty("chinese"); try { chinese = new String(chinese.getBytes("ISO-8859-1"), "GBK"); // 處理中文亂碼 } catch (UnsupportedEncodingException e) { e.printStackTrace(); } System.out.println(username); System.out.println(passsword); System.out.println(chinese); } //讀取XML文件,并處理中文亂碼 public static void readPropertiesFileFromXML(String filename) { Properties properties = new Properties(); try { InputStream inputStream = new FileInputStream(filename); properties.loadFromXML(inputStream); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } String username = properties.getProperty("username"); String passsword = properties.getProperty("password"); String chinese = properties.getProperty("chinese"); //XML中的中文不用處理亂碼,正常顯示 System.out.println(username); System.out.println(passsword); System.out.println(chinese); } //寫(xiě)資源文件,含中文 public static void writePropertiesFile(String filename) { Properties properties = new Properties(); try { OutputStream outputStream = new FileOutputStream(filename); properties.setProperty("username", "myname"); properties.setProperty("password", "mypassword"); properties.setProperty("chinese", "中文"); properties.store(outputStream, "author: shixing_11@sina.com"); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } //寫(xiě)資源文件到XML文件,含中文 public static void writePropertiesFileToXML(String filename) { Properties properties = new Properties(); try { OutputStream outputStream = new FileOutputStream(filename); properties.setProperty("username", "myname"); properties.setProperty("password", "mypassword"); properties.setProperty("chinese", "中文"); properties.storeToXML(outputStream, "author: shixing_11@sina.com"); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
運(yùn)行本程序所需的資源文件,我是放在E盤(pán)根目錄,如E:/readfile.properties
1. readfile.properties username=kh password=kh chinese=謂語(yǔ) 2. writefile.properties #author: shixing_11@sina.com #Fri May 28 22:19:44 CST 2010 password=kh chinese=\u8C13\u8BED username=kh 3. readxmlfile.xml <?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <entry key="password">mypassword</entry> <entry key="chinese">中文</entry> <entry key="username">myname</entry> </properties> 4. writexmlfile.xml <?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <entry key="password">kh</entry> <entry key="chinese">中文</entry> <entry key="username">kh</entry> </properties> 5. readtxtfile.txt username=kh password=kh chinese=中文 6. writetxtfile.txt password=kh chinese=/u4E2D/u6587 username=kh
4.Properties獲取數(shù)據(jù)亂碼解決
1.原因
Properties調(diào)用load(InputStream)時(shí),讀取文件時(shí)使用的默認(rèn)編碼為ISO-8859-1;當(dāng)我們講中文放入到properties文件中,通過(guò)getProperty(key)獲取值時(shí),取到得數(shù)據(jù)是ISO-8859-1格式的,但是ISO-8859-1是不能識(shí)別中文的。
2.解決方法
通過(guò)getProperty()獲取的數(shù)據(jù)data既然是ISO-8859-1編碼的,就通過(guò)data.getByte(“iso-8859-1”)獲取獲取,使用new String(data.getByte(“iso-8859-1”),”UTF-8”)進(jìn)行轉(zhuǎn)換。當(dāng)然properties文件的編碼類型需要和new String(Byte[],charst)中的第二個(gè)參數(shù)的編碼類型相同
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring-Security對(duì)HTTP相應(yīng)頭的安全支持方式
這篇文章主要介紹了Spring-Security對(duì)HTTP相應(yīng)頭的安全支持方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10Mybatis Select Count(*)的返回值類型介紹
這篇文章主要介紹了Mybatis Select Count(*)的返回值類型,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12SpringBoot整合Redis使用@Cacheable和RedisTemplate
本文主要介紹了SpringBoot整合Redis使用@Cacheable和RedisTemplate,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Java中字符串常見(jiàn)題之String相關(guān)講解
今天小編就為大家分享一篇關(guān)于Java中字符串常見(jiàn)題之String相關(guān)講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01基于接口實(shí)現(xiàn)java動(dòng)態(tài)代理示例
這篇文章主要介紹了基于接口實(shí)現(xiàn)java動(dòng)態(tài)代理示例,需要的朋友可以參考下2014-04-04springboot集成@DS注解實(shí)現(xiàn)數(shù)據(jù)源切換的方法示例
本文主要介紹了springboot集成@DS注解實(shí)現(xiàn)數(shù)據(jù)源切換的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03