RandomAccessFile出现中文乱码问题

之前程序里调用了RandomAccessFile的writeByte(String str)方法,报文里存在中文的时候出现了乱码

后面换成了 write(byte b[])或writeBytes(byte b[], int off, int len)结果没有问题了,看了下源码,发现里面主要是对String的getBytes()方法调用出了问题

// 这个是RandomAccessFile类writeByte()方法内部调用的
@Deprecated
public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin)

// 我自己写代码调用的,默认是utf-8编码
public byte[] getBytes()

debug进入程序,发现上面标注过期的方法,丢了几个字节,方法上的注释如下

@deprecated  This method does not properly convert characters into
 bytes.  As of JDK 1.1, the preferred way to do this is via the
{@link #getBytes()} method, which uses the platform's default charset.
此方法无法将字符正确转换为字节。从JDK 1.1开始,执行此操作的首选方法是使用 {@link #getBytes()}方法,该方法使用平台的默认字符集。

猜你喜欢

转载自www.cnblogs.com/xhy-shine/p/10774751.html
今日推荐