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\\爬蟲(chóng)\\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)文章
如何自定義Jackson序列化?@JsonSerialize
這篇文章主要介紹了如何自定義Jackson序列化?@JsonSerialize,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
詳解Java編譯優(yōu)化之循環(huán)展開(kāi)和粗化鎖
之前在講JIT的時(shí)候,有提到在編譯過(guò)程中的兩種優(yōu)化循環(huán)展開(kāi)和粗化鎖,今天從Assembly的角度來(lái)驗(yàn)證一下這兩種編譯優(yōu)化方法,快來(lái)看看吧。2021-06-06
SpringBoot+Mybatis plus+React實(shí)現(xiàn)條件選擇切換搜索實(shí)踐
本文主要介紹了SpringBoot+Mybatis plus+React實(shí)現(xiàn)條件選擇切換搜索實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
MyBatis 動(dòng)態(tài) SQL 優(yōu)化之標(biāo)簽的實(shí)戰(zhàn)與技巧(常見(jiàn)用法)
本文通過(guò)詳細(xì)的示例和實(shí)際應(yīng)用場(chǎng)景,介紹了如何有效利用這些標(biāo)簽來(lái)優(yōu)化 MyBatis 配置,提升開(kāi)發(fā)效率,確保 SQL 的高效執(zhí)行和安全性,感興趣的朋友跟隨小編一起看看吧2025-04-04
IDEA 中創(chuàng)建SpringBoot 父子模塊的實(shí)現(xiàn)
這篇文章主要介紹了IDEA 中創(chuàng)建SpringBoot 父子模塊的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04

