java用字節(jié)數(shù)組解決FileInputStream讀取漢字出現(xiàn)亂碼問(wèn)題
用字節(jié)數(shù)組解決FileInputStream讀取漢字出現(xiàn)亂碼
package hanjia; import java.io.*; //用“字節(jié)數(shù)組”方式讀取文本文件內(nèi)容,然后利用String(byte[] bytes) //或String(byte[] bytes, int offset, int length) 構(gòu)造新字符串來(lái)輸出。 //解決思路:先用較大的字節(jié)數(shù)組讀取文本文件內(nèi)容,將調(diào)用String類構(gòu)造方法將字節(jié)數(shù)組內(nèi)容組合成有意義的漢字 class hanjia { public static void main(String args[]) throws IOException { FileInputStream infile = new FileInputStream("D:/KuGou/f.txt"); try { byte[] b = new byte[128];// 定義一個(gè)字節(jié)數(shù)組 int i = infile.read(b);// 讀取數(shù)據(jù)存放到字節(jié)數(shù)組中,read()返回值-1表示結(jié)束 while (i != -1) {// 讀指針到達(dá)輸出流尾部時(shí)結(jié)束 System.out.print(new String(b, 0, i));//從開(kāi)頭到結(jié)束將字節(jié)數(shù)組內(nèi)容轉(zhuǎn)換為字符串,并輸出 i = infile.read(b);// 讀取后續(xù)數(shù)據(jù)存放到字節(jié)數(shù)組中 } } catch (IOException e) { System.out.println(e.getMessage()); } finally { infile.close();// 關(guān)閉輸入流 } } }
解決FileInputStream讀取ANSI格式txt中文亂碼
GBK中文轉(zhuǎn)為byte后以負(fù)數(shù)開(kāi)頭,正常來(lái)說(shuō)為連續(xù)兩個(gè)負(fù)數(shù),生僻字可能為一個(gè)負(fù)數(shù)和一個(gè)整數(shù),所以需要特殊處理一下
注:utf-8的txt一個(gè)中文占三個(gè)byte數(shù)組,故此方法不適用
import java.io.FileInputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Arrays; public class FileInputStreamTest03 { public static void main(String[] args) { // test String s = "中c國(guó)Dh丄"; byte[] bs = new byte[10]; try { bs = s.getBytes("GBK"); System.out.println(Arrays.toString(bs)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } String s2 = null; try { s2 = new String(bs, "GBK"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } System.out.println(s2); // main FileInputStream fp = null; try { fp = new FileInputStream("C:\\Users\\dell\\Documents\\a代碼備份\\python\\爬蟲\\bilibili\\list.txt"); byte[] b = new byte[10]; int num; while ((num = fp.read(b)) != -1){ int pos = 0; // 記錄負(fù)值個(gè)數(shù),中文GBK為兩個(gè)負(fù)值 for (byte b1 : b) { if (b1 < 0){ pos++; } } // System.out.println(Arrays.toString(b)); if (pos%2 != 0 && b[b.length-1] < 0){ int nextValue=fp.read(); int size = b.length; int nextLen=size+1; //字節(jié)數(shù)組擴(kuò)容一位 b = Arrays.copyOf(b,nextLen); b[size]= (byte) nextValue; String content=new String(b, 0, nextLen, "GBK"); System.out.print(content); } else { System.out.print(new String(b, 0, num, "GBK")); } } } catch (IOException e) { e.printStackTrace(); } finally { if (fp != null){ try { fp.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java datetime數(shù)據(jù)類型去掉時(shí)分秒的案例詳解
在Java中,如果我們想要表示一個(gè)日期而不包括時(shí)間(時(shí)分秒),我們通常會(huì)使用java.time包中的LocalDate類,這篇文章主要介紹了java datetime數(shù)據(jù)類型去掉時(shí)分秒,需要的朋友可以參考下2024-06-06java后端返回?cái)?shù)據(jù)給前端時(shí)去除值為空或NULL的屬性、忽略某些屬性代碼示例
在Java開(kāi)發(fā)中我們處理JSON數(shù)據(jù)時(shí)經(jīng)常會(huì)遇到空值(null)的情況,這篇文章主要給大家介紹了關(guān)于java后端返回?cái)?shù)據(jù)給前端時(shí)去除值為空或NULL的屬性、忽略某些屬性的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07微信公眾號(hào)支付(二)實(shí)現(xiàn)統(tǒng)一下單接口
本篇文章主要給大家介紹調(diào)用微信公眾支付的統(tǒng)一下單API,通過(guò)參數(shù)封裝為xml格式并發(fā)送到微信給的接口地址就可以獲得返回內(nèi)容,需要的朋友可以參考下本文2015-09-09解決nacos升級(jí)spring cloud 2020.0無(wú)法使用bootstrap.yml的問(wèn)題
這篇文章主要介紹了解決nacos升級(jí)spring cloud 2020.0無(wú)法使用bootstrap.yml的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06Springboot下使用Redis管道(pipeline)進(jìn)行批量操作
本文主要介紹了Spring?boot?下使用Redis管道(pipeline)進(jìn)行批量操作,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05springboot的異步任務(wù):無(wú)返回值和有返回值問(wèn)題
這篇文章主要介紹了springboot的異步任務(wù):無(wú)返回值和有返回值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07