62,616
社区成员
发帖
与我相关
我的任务
分享
String str = "乱码的问题";
//然后转换成byte数字
byte[] byteArray = str.getBytes();
//有一个随机数i,从i开始截取这个数组
int i = 6;//随机的
byte[] byteArray2 = new byte[byteArray.length - 1];
for(int j = i; j < byteArray.length; j++) {
byteArray2[j - i] = byteArray[j];
}
String str2;
try {
str2 = new String(byteArray2, "GBK");
System.out.println("----->" + str2);//输出:----->鐨勯棶棰�����������
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
public class Test {
public static void main(String[] args) {
}
/**
*
* @param str 要截取的字符
* @param bytelength 截取的字节长度
* @return
*/
public static String CutString(String str,int bytelength){
String tmp = str.substring(0,6);
while(tmp.getBytes().length>bytelength){
tmp = tmp.substring(0,tmp.length()-1);
}
return tmp;
}
}
while(mMbBuf.get(i)<-30){
i++;
}
//这时候得到的i应该就是汉子的第一个字节码,一般来说汉子的第一个字节码大于-30,而后两个小于-30
//这不是官方资料,是我观察的结果,所以你要多测试
for (j = i; j < 10 + i; j++) {
if(mMbBuf.get(j)<-30){
i++;
}
buf[j - i] = mMbBuf.get(j);
}
MappedByteBuffer mMbBuf = new RandomAccessFile(file, "r").getChannel().map(FileChannel.MapMode.READ_ONLY, 0, 100);
byte[] buf = new byte[10];
int i = 3;
for (j = i; j < 10 + i; j++) {
buf[j - i] = mMbBuf.get(j);
}
String s = new String(buf, "GBK");
System.out.println("----->" + s);