62,614
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) throws UnsupportedEncodingException {
String arr[] = splitByByteSize("abc刘defghijklmnopqrstuvwxyz",4);
int byteLen = 0;
for(int i=0;i<arr.length;i++){
byteLen+=arr[i].getBytes().length;
}
byte[] bytes = new byte[byteLen];
int offset = 0;
for(int i=0;i<arr.length;i++){
System.arraycopy(arr[i].getBytes("utf-8"), 0, bytes, offset, arr[i].getBytes("utf-8").length);
offset += arr[i].getBytes("utf-8").length;
}
System.out.println(">>"+new String(bytes));
}
public static String[] splitByByteSize(String content, int size) throws UnsupportedEncodingException{
byte[] bytes = content.getBytes("utf-8");
int totalSize = bytes.length;
int partNum = 0;
if(totalSize == 0){
return new String[0];
}
if(totalSize % size == 0){
partNum = totalSize / size;
}else{
partNum = totalSize / size + 1;
}
String[] arr = new String[partNum];
int offset = 0;
byte newBytes[] = new byte[size];
for(int i=0;i<partNum-1;i++){
System.arraycopy(bytes, offset, newBytes, 0, size);
arr[i] = new String(newBytes,"utf-8");
offset += size;
}
System.arraycopy(bytes, offset, newBytes, 0, totalSize-offset);
arr[partNum-1] = new String(newBytes,"utf-8");
return arr;
}