StringBuffer s = new StringBuffer();
s.append("张。");//
//s.setLength(10);//设置字符串的长度为10,字符数不能超过10,向里面添加一个中文,一个中文在gbk下是存两个字节
//字节数变为9,
byte[] b = s.toString().getBytes();//将字符串转化为字节数组
if(b.length>0){
return "信息过长";
}
byte[] by = new byte[10];//创建固定长度的字节数组
//将流与字节数组关联
ByteArrayInputStream bs = new ByteArrayInputStream(s.toString().getBytes());
try {
//将字符串信息写入数组中(保证字符串信息存储的都是10个字节)
bs.read(by);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String test = new String(by);
System.out.println(by.length);
System.out.println(s.toString());
System.out.println(test.replace("\0", ""));//将空字符替换掉
return "";
s.append("张。");//
//s.setLength(10);//设置字符串的长度为10,字符数不能超过10,向里面添加一个中文,一个中文在gbk下是存两个字节
//字节数变为9,
byte[] b = s.toString().getBytes();//将字符串转化为字节数组
if(b.length>0){
return "信息过长";
}
byte[] by = new byte[10];//创建固定长度的字节数组
//将流与字节数组关联
ByteArrayInputStream bs = new ByteArrayInputStream(s.toString().getBytes());
try {
//将字符串信息写入数组中(保证字符串信息存储的都是10个字节)
bs.read(by);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String test = new String(by);
System.out.println(by.length);
System.out.println(s.toString());
System.out.println(test.replace("\0", ""));//将空字符替换掉
return "";