我尝试通过 ByteBuffer 访问 byte[] 以表示我定义的文件类型。byte[] 中的第一个位置包含一些元数据并使用位操作进行处理。所以它们根本不代表一个字符。
我想在某个固定位置添加文件数据(例如字符)。
byte[] file_portion
包含一个大文件的一部分:开始部分。其中包括带有元数据的标头。
content
是一个字符串,其中包含我要添加到该缓冲区的信息。start_pos 是从内容中保存新文件数据的第一个位置。
ByteBuffer my_content = ByteBuffer.allocate(this.file_portion.length);
content_buffer.wrap(this.file_portion);
for (int i = 0; i < content.length(); i++) {
char tmp = content.toCharArray()[i];
my_content.put(this.start_pos + i, (byte) tmp)
}
如果我重新映射这个,我会得到一个垃圾和空虚:
CharBuffer debug = my_content.asCharBuffer();
System.out.println("debug " + debug);
我可以理解第一个位置是否显示损坏的字符......但没有一个位置是正确的。