Java讀取properties文件之中文亂碼問題及解決
Java讀取properties文件中文亂碼
初用properties,讀取java properties文件的時候如果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)中文亂碼,因為字節(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() | 從該屬性列表中返回一個不可修改的鍵集,其中鍵及其對應(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文件是一個文本文件
- properties文件的語法有兩種,一種是注釋,一種屬性配置。
注 釋:前面加上#號
屬性配置:以“鍵=值”的方式書寫一個屬性的配置信息。
- properties文件的一個屬性配置信息值可以換行,但鍵不可以換行。值換行用“\”表示。
- properties的屬性配置鍵值前后的空格在解析時候會被忽略。
- properties文件可以只有鍵而沒有值。也可以僅有鍵和等號而沒有值,但無論如何一個屬性配置不能沒有鍵。
eg:
正確的資源文件格式為:

2.功能大致如下
- 讀寫Properties文件
- 讀寫XML文件
- 不僅可以讀寫上述兩類文件,還可以讀寫其它格式文件如txt等,只要符合key=value格式即可.
Properties能讀取以key,value存儲的任何格式文件,看一下他的類結(jié)構(gòu)就知道為什么了

從上面的類結(jié)構(gòu)圖可以看出,它繼承了Hashtable并實現(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();
}
}
}
運行本程序所需的資源文件,我是放在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)時,讀取文件時使用的默認(rèn)編碼為ISO-8859-1;當(dāng)我們講中文放入到properties文件中,通過getProperty(key)獲取值時,取到得數(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)中的第二個參數(shù)的編碼類型相同
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring-Security對HTTP相應(yīng)頭的安全支持方式
這篇文章主要介紹了Spring-Security對HTTP相應(yīng)頭的安全支持方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
Mybatis Select Count(*)的返回值類型介紹
這篇文章主要介紹了Mybatis Select Count(*)的返回值類型,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringBoot整合Redis使用@Cacheable和RedisTemplate
本文主要介紹了SpringBoot整合Redis使用@Cacheable和RedisTemplate,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
springboot集成@DS注解實現(xiàn)數(shù)據(jù)源切換的方法示例
本文主要介紹了springboot集成@DS注解實現(xiàn)數(shù)據(jù)源切換的方法示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03

