工作中在使用byte数组进行文件读取操作的时候,经常遇到byte数组没有填满,导致后面有一大堆\u0000字符,使得显示极为难看。去除byte数组代码如下:
byte[] outputByte = new byte[4096];
String str = new String(outputByte);
Pattern pattern = Pattern.compile("([^\u0000]*)");
Matcher matcher = pattern.matcher(str);
if(matcher.find(0)){
System.out.println(matcher.group(1).getBytes("utf-8"));
}