Java讀取properties文件之中文亂碼問題及解決
Java讀取properties文件中文亂碼
初用properties,讀取java properties文件的時(shí)候如果value是中文,會出現(xiàn)讀取亂碼的問題。
給定country.properties文件如下:
China=中國 USA=美國 Japan=日本
Properties properties = new Properties(); ?
InputStream inputStream = this.getClass().getResourceAsStream("/country.properties"); ?
properties.load(inputStream ); ?
System.out.println(properties.getProperty("China")); ??上面的程序執(zhí)行后的結(jié)果會出現(xiàn)中文亂碼,因?yàn)樽止?jié)流是無法讀取中文的,所以采取reader把inputStream轉(zhuǎn)換成reader用字符流來讀取中文。
代碼如下:
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)常會將我們自定義的配置文件xxx.properties,讀取到我們的Java代碼中去?,F(xiàn)在我目前已知有兩種讀取配置文件的方式,如下所示。
方式一:使用Properties集合工具類讀取配置文件。
Properties的加載方法
| 方法名 | 說明 |
| void load(Reader reader) | 從輸入字符流讀取屬性列表(鍵和元素對) |
| void store(Writer writer, String comments) | 將此屬性列表(鍵和元素對)寫入此Properties表中,一適合使用load(Reader)方法的格式寫入輸出字符流 |
加載完成后根據(jù)下面方法獲取值
| 方法名 | 說明 |
| Object setProperty(String key,String value) | 設(shè)置集合的鍵和值,都是String類型,底層調(diào)用 Hashtable方法put |
| String getProperty(String key) | 使用此屬性列表中指定的鍵搜索屬性 |
| Set<String> stringPropertyNames() | 從該屬性列表中返回一個(gè)不可修改的鍵集,其中鍵及其對應(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ū)動大師目錄\\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)語言環(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讀寫資源文件類properties
Java中讀寫資源文件最重要的類是Properties
1.資源文件要求如下
- properties文件是一個(gè)文本文件
- properties文件的語法有兩種,一種是注釋,一種屬性配置。
注 釋:前面加上#號
屬性配置:以“鍵=值”的方式書寫一個(gè)屬性的配置信息。
- properties文件的一個(gè)屬性配置信息值可以換行,但鍵不可以換行。值換行用“\”表示。
- properties的屬性配置鍵值前后的空格在解析時(shí)候會被忽略。
- properties文件可以只有鍵而沒有值。也可以僅有鍵和等號而沒有值,但無論如何一個(gè)屬性配置不能沒有鍵。
eg:
正確的資源文件格式為:

2.功能大致如下
- 讀寫Properties文件
- 讀寫XML文件
- 不僅可以讀寫上述兩類文件,還可以讀寫其它格式文件如txt等,只要符合key=value格式即可.
Properties能讀取以key,value存儲的任何格式文件,看一下他的類結(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); //寫properties文件
readPropertiesFileFromXML(readxmlfile); //讀取XML文件
writePropertiesFileToXML(writexmlfile); //寫XML文件
readPropertiesFile(readtxtfile); //讀取txt文件
writePropertiesFile(writetxtfile); //寫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);
}
//寫資源文件,含中文
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();
}
}
//寫資源文件到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盤根目錄,如E:/readfile.properties
1. readfile.properties username=kh password=kh chinese=謂語 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文件中,通過getProperty(key)獲取值時(shí),取到得數(shù)據(jù)是ISO-8859-1格式的,但是ISO-8859-1是不能識別中文的。
2.解決方法
通過getProperty()獲取的數(shù)據(jù)data既然是ISO-8859-1編碼的,就通過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)文章
Gradle構(gòu)建基本的Web項(xiàng)目結(jié)構(gòu)
這篇文章主要為大家介紹了Gradle創(chuàng)建Web項(xiàng)目基本的框架結(jié)構(gòu)搭建,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03
Spring Security 實(shí)現(xiàn)多種登錄方式(常規(guī)方式外的郵件、手機(jī)驗(yàn)證碼登錄)
本文主要介紹了Spring Security 實(shí)現(xiàn)多種登錄方式(常規(guī)方式外的郵件、手機(jī)驗(yàn)證碼登錄),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
java?9大性能優(yōu)化經(jīng)驗(yàn)總結(jié)
這篇文章主要介紹了java?9大性能優(yōu)化經(jīng)驗(yàn)總結(jié),包括:Java代碼優(yōu)化,數(shù)據(jù)庫優(yōu)化,分布式緩存,異步化,Web前段,搜索引擎優(yōu)化等需要的朋友可以參考下2023-02-02
在Java生產(chǎn)環(huán)境下進(jìn)行性能監(jiān)控與調(diào)優(yōu)的詳細(xì)過程
在Java生產(chǎn)環(huán)境下進(jìn)行性能監(jiān)控與調(diào)優(yōu)是一個(gè)復(fù)雜但重要的過程,它涉及到多個(gè)方面,包括代碼分析、JVM監(jiān)控、線程管理、垃圾收集優(yōu)化、內(nèi)存管理、數(shù)據(jù)庫交互等,下面我將提供一個(gè)詳細(xì)的概述和示例代碼,需要的朋友可以參考下2025-02-02
springboot全局配置文件與多環(huán)境配置的全過程
SpringBoot項(xiàng)目在多環(huán)境配置上表現(xiàn)的非常優(yōu)秀,只需要非常簡單的操作就可以完成配置,下面這篇文章主要給大家介紹了關(guān)于springboot全局配置文件與多環(huán)境配置的相關(guān)資料,需要的朋友可以參考下2021-12-12

