Reader和InputStream有什么区别? [英] What is the difference between Reader and InputStream?

查看:156
本文介绍了Reader和InputStream有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Reader和InputStream有什么区别?
何时使用什么?
如果我可以使用Reader读取字符,为什么我将使用inputstream,我想读取对象?

解决方案

InputStream是从资源获取信息的原始方法。它逐字节地获取数据,而不执行任何类型的转换。如果您正在读取图像数据或任何二进制文件,则这是要使用的流。



读者专为字符流而设计。如果您正在阅读的信息全部为文本,则Reader将为您处理字符解码,并从原始输入流中为您提供unicode字符。如果您正在阅读任何类型的文本,这是要使用的流。



您可以使用InputStreamReader类包装InputStream并将其转换为Reader。 p>

  Reader reader = new InputStreamReader(inputStream,StandardCharsets.UTF_8); 


What is the difference between Reader and InputStream? And when to use what? If I can use Reader for reading characters why I will use inputstream, I guess to read objects?

解决方案

An InputStream is the raw method of getting information from a resource. It grabs the data byte by byte without performing any kind of translation. If you are reading image data, or any binary file, this is the stream to use.

A Reader is designed for character streams. If the information you are reading is all text, then the Reader will take care of the character decoding for you and give you unicode characters from the raw input stream. If you are reading any type of text, this is the stream to use.

You can wrap an InputStream and turn it into a Reader by using the InputStreamReader class.

Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);

这篇关于Reader和InputStream有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆