java讀取文件里面部分漢字內(nèi)容亂碼的解決方案
java讀取文件里面部分漢字內(nèi)容亂碼
讀取一個txt文件,到代碼中打印出來,發(fā)票有部分漢字的內(nèi)容是亂碼的。
我開始的方式是這樣的, 如下,這是完全錯誤的,漢字是兩個字節(jié)的,如果每次讀固定個字節(jié),可能會把漢字截斷。
就會出現(xiàn)部分亂碼的情況。
package susq.path; import java.io.File; import java.io.FileInputStream; import java.io.IOException; /** * @author susq * @since 2018-05-18-19:28 */ public class WrongMethodReadTxt { public static void main(String[] args) throws IOException { ClassLoader classLoader = WrongMethodReadTxt.class.getClassLoader(); String filePath = classLoader.getResource("").getPath() + "/expect1.txt"; System.out.println(filePath); File file = new File(filePath); try (FileInputStream in = new FileInputStream(file)) { byte[] bytes = new byte[1024]; StringBuffer sb = new StringBuffer(); int len; while ((len = in.read(bytes)) != -1) { sb.append(new String(bytes, 0, len)); } System.out.println(sb.toString()); } } }
如果存在漢字,就要按字符的方式讀?。?/p>
package susq.path; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; /** * @author susq * @since 2018-05-18-17:39 */ public class SysPath { public static void main(String[] args) throws IOException { ClassLoader classLoader = SysPath.class.getClassLoader(); String filePath = classLoader.getResource("").getPath() + "/expect1.txt"; System.out.println(filePath); File file = new File(filePath); try (BufferedReader br = new BufferedReader(new FileReader(file))) { StringBuffer sb = new StringBuffer(); while (br.ready()) { sb.append(br.readLine()); } System.out.println(sb); } } }
java的IO流讀取數(shù)據(jù)時,解決中文亂碼,還有個別中文亂碼問題
情況:用IO流讀取數(shù)據(jù)時,若是不設置編碼格式,出來的數(shù)據(jù)未必是我們所要的
解決:讀取數(shù)據(jù)時,設置編碼
代碼:(字符串設置對應的編碼即可,但這種方式,會導致個別中文亂碼,貌似是byte[]導致的)
//這里我通過socket方式,獲取流,并讀取數(shù)據(jù) //代理需要外置配置(代理配置需要判斷,若有配置,則添加,若無配置,則不添加) Socket socket = new Socket("192.168.99.100", 80); String url = "GET " + href + " HTTP/1.1\r\n\r\n"; socket.getOutputStream().write(new String(url).getBytes()); InputStream is = socket.getInputStream(); byte[] bs = new byte[1024]; int i; StringBuilder str = new StringBuilder(); while ((i = is.read(bs)) > 0) { //一定要加編碼,不然,在輸出到文件時,部分數(shù)據(jù)會亂 str.append(new String(bs, 0, i,"UTF-8")); //由于socket讀取不會斷開,所以只能自斷開連接讀取 if(new String(bs, 0, i,"UTF-8").contains("</html>")){ break; } }
解決個別中文亂碼問題:
代碼:
//代理需要外置配置(代理配置需要判斷,若有配置,則添加,若無配置,則不添加) Socket socket = new Socket("192.168.99.100", 80); //Socket socket = new Socket(); String url = "GET " + href + " HTTP/1.1\r\n\r\n"; socket.getOutputStream().write(new String(url).getBytes()); InputStream is = socket.getInputStream(); //解決個別中文亂碼 StringBuilder str = new StringBuilder(""); InputStreamReader isr = new InputStreamReader(is,"UTF-8"); BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) { str.append(line + "\n"); if(line.contains("</html>")){ break; } }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
TransmittableThreadLocal線程間傳遞邏輯示例解析
這篇文章主要介紹了TransmittableThreadLocal線程間傳遞邏輯示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06SpringCloud之Feign遠程接口映射的實現(xiàn)
這篇文章主要介紹了SpringCloud之Feign遠程接口映射的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09高內(nèi)聚低耦合原則_動力節(jié)點Java學院整理
耦合度就是某模塊(類)與其它模塊(類)之間的關聯(lián)、感知和依賴的程度,是衡量代碼獨立性的一個指標,也是軟件工程設計及編碼質(zhì)量評價的一個標準2017-08-08從SpringBoot打war包并配置外部Tomcat運行的全流程
由于其他原因,我們需要使用SpringBoot打成war包放在外部的Tomcat中運行,本文就以一個案例來說明從SpringBoot打war包到Tomcat配置并運行的全流程經(jīng)過,需要的朋友可以參考下2024-06-06