Java Web 学习笔记之六 DataOutputStream方法writeBytes(String s)方法中文乱码问题

简介

java.io.DataOutputStream是java流中一个比较好用的类,可以直接通过其中的方法向输出流(文件、网络等)输出字串、整形、浮点等数据结构数据。

但是,其中的writeBytes方法,当中文字串直接作为参数传入之后,实际输出流输出的内容却是乱码,这是为何呢?

我们来看看该方法的源码:

/**
 * Writes out the string to the underlying output stream as a
 * sequence of bytes. Each character in the string is written out, in
 * sequence, by discarding its high eight bits. If no exception is
 * thrown, the counter <code>written</code> is incremented by the
 * length of <code>s</code>.
 *
 * @param      s   a string of bytes to be written.
 * @exception  IOException  if an I/O error occurs.
 * @see        java.io.FilterOutputStream#out
 */
public final void writeBytes(String s) throws IOException {
	int len = s.length();
	for (int i = 0 ; i < len ; i++) {
		out.write((byte)s.charAt(i));
	}
	incCount(len);
}
其中的注释中有一句话: Each character in the string is written out, in sequence, by discarding its high eight bits.意思是字串中的字符输出形式是将字节中的低8位输出,如果有高8位会丢弃之。

意思就是它将char强制类型转换成了byte,char是16位的,byte是8位的。
我们都知道英文字符是8位的,而中文字符是16位的,因此英文字串不会出现乱码,而占用位数更多的16位中文字符就被丢弃了高八位,最后成为了乱码。

解决方法:

调用 java.lang.String.getBytes()方法,然后调用:
write(str.getBytes());就行了,write(byte b[])方法源码如下:

/**
 * Writes <code>b.length</code> bytes to this output stream.
 * <p>
 * The <code>write</code> method of <code>FilterOutputStream</code>
 * calls its <code>write</code> method of three arguments with the
 * arguments <code>b</code>, <code>0</code>, and
 * <code>b.length</code>.
 * <p>
 * Note that this method does not call the one-argument
 * <code>write</code> method of its underlying stream with the single
 * argument <code>b</code>.
 *
 * @param      b   the data to be written.
 * @exception  IOException  if an I/O error occurs.
 * @see        java.io.FilterOutputStream#write(byte[], int, int)
 */
public void write(byte b[]) throws IOException {
	write(b, 0, b.length);
}


  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值