Java字节流乱码问题
不能用字符流读取文件,需要用字节流读取文件,该文件中含有中文,英文,键盘上的所有字符,在读取的时候,设置的缓冲区是1024,那么就有可能出现一种问题,在读取到1023字节的时候都是英文和数字,刚好一个字符一个字节,这个时候碰到一个汉字,就只读取了汉字的其中一个字节,就出现了乱码.
以下方案不行:
读取到\r\n或者是\n,为什么可能根本就没有换行这个字符写入到文件.
快过年了给个吉利分数数字88,恭祝各位:
新年到、春节到、有成绩、别骄傲、失败过、别死掉、齐努力、开大炮、好运气、天上掉、同分享、大家乐。天天好运道,日日福星照! 加入尾部(汉字字头)识别,然后再用个全局变量记录这个尾部。如果是汉字字头,就加入下一组中。
[解决办法]
int size_btCat = 0;
for(int i = 0;;i ++){
if(m_btCat[i] == 0){
size_btCat = i;
break;
}
}
//when we scan chinese code, because one chinese word need 3bytes,
//so we should get all buffer to change to the string at end
if(buffer[size - 1] == '\r'){
for(int i=0; i < size; i ++){
int tmpSize = size_btCat;
m_btCat[tmpSize + i] = buffer[i];
}
String tmpStrSec = new String(m_btCat, 0, m_btCat.length);
Arrays.fill(m_btCat,(byte)0);
postBroadcast(tmpStrSec);
}
else{
//if string is bigger than 32bytes, we should concat not only times
for(int i=0; i < size; i ++){
int tmpSize = size_btCat;
m_btCat[tmpSize + i] = buffer[i];
}
//m_strCat = m_strCat.concat(str);
}
[解决办法]