处理使用ByteArrayOutputStream读取文件中文乱码情况

最开始:
ByteArrayOutputStream baos=new ByteArrayOutputStream();
int length=0;
byte[] buffer=new byte[1024];
while((length=is.read(buffer))!=-1){
baos.write(buffer, 0, length);
}
is.close();
baos.close();
Thread thread = new IPSErTask(baos.toString());
这样的处理会导致文件中的中文乱码。
解决方案如下:
将读取到的数据强制转化成UTF-8
ByteArrayOutputStream baos=new ByteArrayOutputStream();
int length=0;
byte[] buffer=new byte[1024];
while((length=is.read(buffer))!=-1){
baos.write(buffer, 0, length);
}
is.close();
baos.close();
byte[] lens = baos.toByteArray();20190312
String result = new String(lens,“UTF-8”);
Thread thread = new IPSErTask(result)
这样处理之后乱码问题就解决了。

你可能感兴趣的:(处理使用ByteArrayOutputStream读取文件中文乱码情况)