且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Java 中将 ANSI 字符转换为 UTF-8

更新时间:2023-09-11 22:02:04

此错误不是由字符编码引起的.表示UTF数据的长度有误.

This error is not caused by character encoding. It means the length of the UTF data is wrong.

刚刚意识到这是一个写入错误,而不是读取错误.

Just realized this is a writing error, not reading error.

UTF 长度只有 2 个字节,所以它只能容纳 64K UTF-8 字节.您正在尝试写入 100K,这是行不通的.

The UTF length is only 2 bytes so it can only hold 64K UTF-8 bytes. You are trying to writing 100K, it's not going to work.

这个限制是硬编码的,没有办法绕过这个,

This limit is hardcoded and no way to get around this,

if (utflen > 65535)
    throw new UTFDataFormatException(
            "encoded string too long: " + utflen + " bytes");