比较String 字符串的字节大小

Posted joker331

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了比较String 字符串的字节大小相关的知识,希望对你有一定的参考价值。

package com.ittx.edi.erp;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class test001 {

public static void main(String[] args) {
try {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("你");

while (stringBuilder.toString().getBytes("utf-8").length < 4194304){ //比较字节是否小于4M
System.out.println(stringBuilder.toString().getBytes("utf-8").length);
stringBuilder.append("你你你你你你你");
}

generateNewText("C:\temp.txt", stringBuilder.toString());
}catch (Exception ex){

}


}

public static File generateNewText(String filePath, String content) {
try {
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(content);
fileWriter.close();
return file;
} catch (Exception ex) {
return null;
}

}

}

以上是关于比较String 字符串的字节大小的主要内容,如果未能解决你的问题,请参考以下文章