Java讀寫.properties文件解決中文亂碼問題
一般使用到properties配置文件,一般都是在spring項目里面,直接由框架幫你讀,當然,你也得考慮到編碼的問題。
但是現在要是要求使用Java直接讀寫properties文件,就發(fā)現很多的問題,比如,我的properties文件的編碼竟然不是utf-8的。或者說我壓根就沒考慮到這個問題。
再比如,當properties文件里面有漢子的時候,發(fā)現讀寫的漢字亂碼了,在我這是因為我的電腦默認編碼是gbk,但是讀的時候,又沒有設置編碼,搞出的問題。
下面直接上代碼,看問題。
package com.lxk.propertyFileTest; import java.io.*; import java.util.Properties; /** * 讀寫properties文件測試 * <p> * Created by lxk on 2017/4/25 */ public class Main { public static void main(String[] args) { Properties prop = new Properties(); InputStream in = null; FileOutputStream oFile = null; try { in = new BufferedInputStream(new FileInputStream("D:config.properties")); //prop.load(in);//直接這么寫,如果properties文件中有漢子,則漢字會亂碼。因為未設置編碼格式。 prop.load(new InputStreamReader(in, "utf-8")); for (String key : prop.stringPropertyNames()) { System.out.println(key + ":" + prop.getProperty(key)); } //保存屬性到b.properties文件 oFile = new FileOutputStream("b.properties", false);//true表示追加打開,false每次都是清空再重寫 prop.setProperty("phone", "10086"); //prop.store(oFile, "此參數是保存生成properties文件中第一行的注釋說明文字");//這個會兩個地方亂碼 //prop.store(new OutputStreamWriter(oFile, "utf-8"), "漢字亂碼");//這個就是生成的properties文件中第一行的注釋文字亂碼 prop.store(new OutputStreamWriter(oFile, "utf-8"), "lll"); } catch (Exception e) { System.out.println(e.getMessage()); } finally { if (in != null) { try { in.close(); } catch (IOException e) { System.out.println(e.getMessage()); } } if (oFile != null) { try { oFile.close(); } catch (IOException e) { System.out.println(e.getMessage()); } } } } }
運行結果:這個只是讀出來的內容的結果。
下面是寫出來的文件內容。
額,這個圖,有點亂。但是,卻把三種運行情況,全部給展示出來了。很清晰。
最后,代碼里面也看到了怎么把字節(jié)流變成帶編碼格式的字符流,這個可以注意下,我也留個筆記。
對上面的代碼的更新,算是結構調整,功能分開。瞬間代碼看著就清晰明了啦。
所以,一般上面的代碼是不推薦實用的。個中妙用,自行體會吧。
package com.lxk.propertyFileTest; import java.io.*; import java.util.Properties; /** * 讀寫properties文件測試 * <p> * Created by lxk on 2017/4/25 */ public class Main { public static void main(String[] args) { Properties prop = readPropertiesFile(); writePropertiesFile(prop); } /** * 讀Properties文件 */ private static Properties readPropertiesFile() { Properties prop = new Properties(); InputStream in = null; try { in = new BufferedInputStream(new FileInputStream("D:config.properties")); //prop.load(in);//直接這么寫,如果properties文件中有漢子,則漢字會亂碼。因為未設置編碼格式。 prop.load(new InputStreamReader(in, "utf-8")); for (String key : prop.stringPropertyNames()) { System.out.println(key + ":" + prop.getProperty(key)); } } catch (Exception e) { System.out.println(e.getMessage()); } finally { if (in != null) { try { in.close(); } catch (IOException e) { System.out.println(e.getMessage()); } } } return prop; } /** * 寫Properties文件 */ private static void writePropertiesFile(Properties prop) { prop.setProperty("phone", "10086"); FileOutputStream oFile = null; try { //保存屬性到b.properties文件 oFile = new FileOutputStream("b.properties", false);//true表示追加打開,false每次都是清空再重寫 //prop.store(oFile, "此參數是保存生成properties文件中第一行的注釋說明文字");//這個會兩個地方亂碼 //prop.store(new OutputStreamWriter(oFile, "utf-8"), "漢字亂碼");//這個就是生成的properties文件中第一行的注釋文字亂碼 prop.store(new OutputStreamWriter(oFile, "utf-8"), "lll"); } catch (Exception e) { System.out.println(e.getMessage()); } finally { if (oFile != null) { try { oFile.close(); } catch (IOException e) { System.out.println(e.getMessage()); } } } } }
注意:這個是我后來發(fā)現的,不知道在看的各位有沒有這個問題。
我發(fā)現寫出來的properties文件的編碼格式并不是簡單的utf-8,而是utf-8無bom格式。證據可參見下圖:
這個打開工具叫 Notepad++ 估計在看的各位的電腦上都有這個吧。
但是你要是把這個文件的編碼格式給修改成utf-8編碼之后,運行的結果,就有一丟丟不一樣。
繼續(xù)參見下圖:
看到多了一個小杠“”-“”,具體怎么解釋,我暫時還不清楚。
這個時候,寫出來的文件的,也同樣出現了這個問題,具體還是繼續(xù)參見下圖:
所以,這個我暫時解釋不了。
慚愧。。。。
還有個問題就是:讀出來的屬性,是不按原來文件中的順序展示的,當然寫的時候,也是亂序的。
這還是個問題,還有待解決。什么時候解決了,再在此處留個鏈接。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
IntelliJ IDEA下SpringBoot如何指定某一個配置文件啟動項目
這篇文章主要介紹了IntelliJ IDEA下SpringBoot如何指定某一個配置文件啟動項目問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09SpringBoot?整合Security權限控制的初步配置
這篇文章主要為大家介紹了SpringBoot?整合Security權限控制的初步配置實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11解決springSecurity 使用默認登陸界面登錄后無法跳轉問題
這篇文章主要介紹了解決springSecurity 使用默認登陸界面登錄后無法跳轉問題,項目環(huán)境springboot下使用springSecurity 版本2.7.8,本文通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧2023-12-12Mybatis入門教程(四)之mybatis動態(tài)sql
這篇文章主要介紹了Mybatis入門教程(四)之mybatis動態(tài)sql的相關資料,涉及到動態(tài)sql及動態(tài)sql的作用知識,本文介紹的非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09