Java 字节流和字符流

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3013996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:34:46  来源:igfitidea点击:

Byte Stream and Character stream

javafilestreamiostream

提问by JavaUser

Please explain what Byte streams and Character streams are. What exactly do these mean? Is a Microsoft Word document Byte oriented or Character oriented?

请解释什么是字节流和字符流。这些究竟是什么意思?Microsoft Word 文档是面向字节还是面向字符?

Thanks

谢谢

采纳答案by kgiannakakis

A stream is a way of sequentially accessing a file. A byte stream access the file byte by byte. A byte stream is suitable for any kind of file, however not quite appropriate for text files. For example, if the file is using a unicode encoding and a character is represented with two bytes, the byte stream will treat these separately and you will need to do the conversion yourself.

流是一种顺序访问文件的方式。字节流逐字节访问文件。字节流适用于任何类型的文件,但不太适合文本文件。例如,如果文件使用 unicode 编码并且一个字符用两个字节表示,则字节流将分别处理它们,您需要自己进行转换。

A character stream will read a file character by character. A character stream needs to be given the file's encoding in order to work properly.

字符流将逐个字符地读取文件。字符流需要被赋予文件的编码才能正常工作。

Although a Microsoft Word Document contains text, it can't be accessed with a character stream (it isn't a text file). You need to use a byte stream to access it.

尽管 Microsoft Word 文档包含文本,但无法通过字符流(它不是文本文件)访问。您需要使用字节流来访问它。

回答by dty

Read this. It tells you about the difference between bytes and characters (as well as loads of other useful stuff)

这个。它告诉您字节和字符之间的区别(以及大量其他有用的东西)

回答by Urjit

1.Character oriented are tied to datatype. Only string type or character type can be read through it while byte oriented are not tied to any datatype, data of any datatype can be read(except string) just you have to specify it.

1.面向字符与数据类型相关。它只能读取字符串类型或字符类型,而面向字节不绑定任何数据类型,可以读取任何数据类型的数据(字符串除外),只需指定它即可。

2.Character oriented reads character by character while byte oriented reads byte by byte

2.面向字符逐个字符读取,而面向字节逐字节读取

3.Character oriented streams use character encoding scheme(UNICODE) while byte oriented do not use any encoding scheme

3.面向字符的流使用字符编码方案(UNICODE),而面向字节的流不使用任何编码方案

4.Character oriented streams are also known as reader and writer streams Byte oriented streams are known as data streams-Data input stream and Data output stream

4.面向字符的流也称为读写器流面向字节的流称为数据流-数据输入流和数据输出流

回答by Ravindra babu

ByteStreams:

字节流

From oracle documentation page about byte streams:

来自关于字节流的oracle 文档页面:

Programs use byte streams to perform input and output of 8-bit bytes. All byte stream classes are descended from InputStreamand OutputStream.

程序使用字节流来执行 8 位字节的输入和输出。所有字节流类都来自InputStreamOutputStream

enter image description here

在此处输入图片说明

When to use:

何时使用:

Byte streams should only be used for the most primitive I/O

字节流应该只用于最原始的 I/O

When not to use:

何时不使用:

You should not use Byte stream to read Character streams

你不应该使用字节流来读取字符流

e.g. To read a text file

例如读取文本文件

Character Streams:

字符流:

From oracle documentation page about character streams:

来自关于字符流的oracle 文档页面:

The Java platform stores character values using Unicode conventions. Character stream I/O automatically translates this internal format to and from the local character set.

Java 平台使用 Unicode 约定存储字符值。字符流 I/O 自动将此内部格式转换为本地字符集或从本地字符集转换。

All character stream classes are descended from Readerand Writer.

所有字符流类都来自ReaderWriter

Character streams are often "wrappers" for byte streams. The character stream uses the byte stream to perform the physical I/O, while the character stream handles translation between characters and bytes.

字符流通常是字节流的“包装器”。字符流使用字节流来执行物理 I/O,而字符流则处理字符和字节之间的转换。

There are two general-purpose byte-to-character "bridge" streams: InputStreamReaderand OutputStreamWriter.

有两个通用的字节到字符“桥接”流:InputStreamReaderOutputStreamWriter.

When to use:

何时使用:

To read character streams either from Socketor Fileof characters

从字符SocketFile字符中读取字符流

In Summary:

总结

Byte streamreads and write a byte at a time. We must avoid the usage of byte stream while dealing with more sophisticated data.

字节流一次读取和写入一个字节。在处理更复杂的数据时,我们必须避免使用字节流。

Character Streamand other available streams should be used to handle sophisticated data.

应该使用字符流和其他可用的流来处理复杂的数据。

回答by V.Dev

A character stream will read a file character by character. The character streams are capable to read 16-bit characters (byte streams read 8-bit characters). Character streams are capable to translate implicitly 8-bit data to 16-bit data or vice versa. Character stream can support all types of character sets ASCII, Unicode, UTF-8, UTF-16 etc.But byte stream is suitable only for ASCII character set.The Java platform stores character values using Unicode conventions. Character stream I/O automatically translates this internal format to and from the local character set.

字符流将逐个字符地读取文件。字符流能够读取 16 位字符(字节流读取 8 位字符)。字符流能够将 8 位数据隐式转换为 16 位数据,反之亦然。字符流可以支持ASCII、Unicode、UTF-8、UTF-16等所有类型的字符集。但是字节流只适用于ASCII字符集。Java平台使用Unicode约定存储字符值。字符流 I/O 自动将此内部格式转换为本地字符集或从本地字符集转换。

Unless you are working with binary data, such as image and sound files, you should use readers and writers to read and write information with character streams.

除非您使用二进制数据,例如图像和声音文件,否则您应该使用读取器和写入器通过字符流读取和写入信息。