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()}方法,该方法使用平台的默认字符集。

 

转载于:https://www.cnblogs.com/xhy-shine/p/10774751.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值