Reader/Writer与InputStream/OutputStream的区别

本文探讨了Java中输入输出流(IO流)与字符流(Reader/Writer)之间的核心区别。主要聚焦于它们的设计理念,即如何通过使用字符而非字节来支持国际化,并介绍了一些关键的方法差异及其在实际应用中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Readers and writers are like input streams and output streams. The primary difference lies in the
fundamental datatype that is read or written; streams are byte-oriented, whereas readers and
writers use characters and strings.

2. The reason for this is internationalization. Readers and writers were designed to allow programs
to use a localized character set and still have a stream-like model for communicating with
external devices.

3. These are analogous to the read( ) methods defined for InputStream. For example, read( )
still returns an integer. The difference is that, instead of data values being in the range of 0-255
(i.e., single bytes), the return value is in the range of 0-65535 (appropriate for characters, which
are 2 bytes wide).

4. Instead, readers and writers can be used as a layer on top of streams™ most readers have a
constructor that takes an InputStream as an argument, and most writers have a constructor
that takes an OutputStream as an argument.

FileOutputStream destination = new FileOutputStream(fileName);
BufferedOutputStream bufferedDestination = new BufferedOutputStream(destination);
GZIPOutputStream zippedDestination = new GZIPOutputStream(bufferedDestination);
OutputStreamWriter destinationWriter = new OutputStreamWriter(zippedDestination);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值