简体   繁体   English

java中字符流的作用是什么?

[英]what is the use of character stream in java?

what is the use of character stream in java? java中字符流的作用是什么? why we need while there is byte streams?I hav gone through many sites i dint get it clearly...Please make me clear...please dont paste google results为什么我们在有字节流时需要?我浏览了很多网站我都没有清楚...请让我清楚...请不要粘贴谷歌搜索结果

I assume you mean theReader interface?我假设您是说Reader界面? If you're working with text, you don't want to have to worry about binary/text conversions at every layer.如果您正在处理文本,则不必担心每一层的二进制/文本转换。 Indeed, you may not even have binary data to convert, conceptually - look atStringReader for example.实际上,从概念上讲,您甚至可能没有要转换的二进制数据——例如,看看StringReader

Having the Reader interface makes it clear that you really want text, and that the binary data underlying that text is irrelevant to you, so long as it can be provided as a sequence of Unicode characters somehow.拥有Reader界面可以清楚地表明您确实想要文本,并且该文本下的二进制数据与您无关,只要它可以以某种方式作为 Unicode 字符序列提供。

It's worth being very clear in your mind that binary and text are different.值得在您心中非常清楚二进制和文本是不同的。 If you try to treat arbitrary binary data as text (eg trying to read an image file into a string) you will lose information.如果您尝试将任意二进制数据视为文本(例如,尝试将图像文件读入字符串),您将丢失信息。 Why would you not want different types to deal with the different forms of data?为什么希望不同类型来处理不同形式的数据?

It's a bit like saying, "We could store the data for every object in a byte array, and convert pieces of that as and when we need to."这有点像说,“我们可以将每个对象的数据存储在一个字节数组中,并在需要时转换其中的部分。” Well yes, we could (if you could convert opaque binary data to/from a reference) but it would be hideous in terms of encapsulation.是的,我们可以(如果您可以将不透明的二进制数据转换为引用或从引用转换),但在封装方面会很糟糕。 The Reader interface is a layer of encapsulation, allowing various data sources to expose character data (whether they need to decode binary data or not), and other code to consume that character data, without having to be aware of exactly where it comes from. Reader接口是一层封装,允许各种数据源公开字符数据(无论是否需要解码二进制数据)和其他代码来使用该字符数据,而不必知道它的确切来源。

I'm sorry, but google results actually answer your question.对不起,但谷歌结果实际上回答了你的问题。 From Sun's website (first google hit for character stream java ):Sun 的网站(第一次谷歌搜索character stream java ):

Why use character streams?为什么要使用字符流? The primary advantage of character streams is that they make it easy to write programs that are not dependent upon a specific character encoding, and are therefore easy to internationalize.字符流的主要优点是它们可以轻松编写不依赖于特定字符编码的程序,因此易于国际化。

A second advantage of character streams is that they are potentially much more efficient than byte streams.字符流的第二个优点是它们可能比字节流更高效。 The implementations of many of Java's original byte streams are oriented around byte-at-a-time read and write operations.许多 Java 原始字节流的实现都是围绕着一次一个字节的读写操作进行的。 The character-stream classes, in contrast, are oriented around buffer-at-a-time read and write operations.相比之下,字符流类面向一次缓冲区的读写操作。 This difference, in combination with a more efficient locking scheme, allows the character stream classes to make up for the added overhead of encoding conversion in many cases.这种差异与更有效的锁定方案相结合,允许字符流类在许多情况下弥补编码转换的额外开销。

Character streams are handy way to deal with character data like text file.字符流是处理文本文件等字符数据的便捷方式。 You can take take a byte stream and supply it a character encoding and effectively convert it into with character stream and similarly you can take a character stream and supply a character encoding and effectively convert it into a byte stream.您可以采用字节流并为其提供字符编码并将其有效地转换为字符流,类似地,您可以采用字符流并提供字符编码并将其有效地转换为字节流。

The conversion usually done by decorating streams.转换通常通过装饰流来完成。 As you may guess, a character encoding is an algorithm to convert chars to bytes and vice-versa.正如您可能猜到的,字符编码是一种将字符转换为字节的算法,反之亦然。

At the risk of sounding sarcastic its purpose is to read a stream of characters.冒着听起来讽刺的风险,它的目的是读取字符流。

The point is that characters are not the same as bytes.关键是字符与字节不同。

A byte is a collection of 8 bits the only varation possible being whether its big-endian or little-endian.一个字节是 8 位的集合,唯一可能的变化是它的大端或小端。

A character is a more complex little beast.角色是更复杂的小野兽。 It belongs to a character set and is affected by National Language Settings.它属于一个字符集,并受国家语言设置的影响。

The simplest is ASCII with NLS set to "C" which is pretty much identical to a bytes, except that the values have specific meano=ings eg x'30' is ASCII character '0' which will return true if an 'isNumber()' method is applied.最简单的是将 NLS 设置为“C”的 ASCII,它与字节几乎相同,除了值具有特定的意思=ings 例如 x'30' 是 ASCII 字符 '0',如果'isNumber() 将返回真' 方法被应用。 Next up the scale are the various ISO eight bit code pages which shuffle the character assignments above x'7F' around normally to handle european accented characters.接下来是各种 ISO 八位代码页,这些代码页通常会在 x'7F' 上方随机排列字符分配,以处理欧洲重音字符。

In addition there are other encodings such as EBCDIC which are still used widely -- here a '0' is encoded as x'F0'.此外,还有其他编码,例如 EBCDIC,它们仍然被广泛使用——这里的“0”被编码为 x“F0”。

Then there is unicode-16 which will en-code several thousand characters from various widly used alphabets and unicode-32 which encodes several million characters from widely and not so widely used (eg Klingon and Mycinean) alphabets.然后是 unicode-16 它将编码来自各种广泛使用的字母表中的数千个字符和 unicode-32 它将编码来自广泛使用和不那么广泛使用的(例如 Klingon 和 Mycinean)字母表中的数百万个字符。

There is the intermediate utf-8 encodeing which leaves plain old ASCII characters as is coded in 7 bits but also has fiendishly clever algorithms for storing the entire unicode alphabet in two, three or four bytes.有中间的 utf-8 编码,它留下了 7 位编码的普通旧 ASCII 字符,但也有非常聪明的算法,用于将整个 unicode 字母表存储在两个、三个或四个字节中。

In addition there are "legacy" far eastern coding schemes for Japanese and chinese characters which have complex schemes for indicating if a character is held in one, two or three bytes.此外,还有用于日文和中文字符的“传统”远东编码方案,它们具有复杂的方案,用于指示字符是否包含在一个、两个或三个字节中。

The point is that that character stream class knows about all these code pages and can do intelligent things with character input such as convert cp874 to unicode-16 which the byte stream classes cannot.关键是该字符流类知道所有这些代码页,并且可以对字符输入执行智能操作,例如将 cp874 转换为 unicode-16,而字节流类则不能。

是的,可以使用 InputStream/OutputStream 处理文本数据,但 Reader/Writer 接口提供了处理文本数据的有用方法。

I think it's just a matter of following a hierarchy.我认为这只是遵循等级制度的问题。 The same reason there are strings, even though you could simply use a character array instead of a string.与字符串的原因相同,即使您可以简单地使用字符数组而不是字符串。

Essentially the issue is that "a byte" does not map well to "a character".本质上,问题在于“一个字节”不能很好地映射到“一个字符”。 Unicode specifies more characters than fit in 16 bits, so 8 bits are not enough. Unicode 指定的字符多于 16 位,因此 8 位是不够的。

There are more reasons but that is the primary one.还有更多原因,但这是首要原因。

The Java platform stores character values using Unicode conventions. Java 平台使用 Unicode 约定存储字符值。 Character I/O stream automatically translates this internal format to and from the local character set.字符 I/O 流自动将此内部格式转换为本地字符集或从本地字符集转换。 In Western locales, the local character set is usually an 8-bit superset of ASCII.在西方语言环境中,本地字符集通常是 ASCII 的 8 位超集。

For most applications, I/O with character stream is no more complicated than I/O with binary streams.对于大多数应用程序,字符流的 I/O 并不比二进制流的 I/O 复杂。 Input and output done with stream classes automatically translates to and from the local character set.使用流类完成的输入和输出会自动与本地字符集相互转换。

A program that uses character stream in place of byte streams automatically adapts to the local character set and is ready for internationalisation - all without extra effort by the programmer.使用字符流代替字节流的程序会自动适应本地字符集,并为国际化做好准备——这一切都不需要程序员额外的努力。

All character stream classes are descended from Reader and Writer.所有字符流类都继承自 Reader 和 Writer。 As with byte streams, there are character stream classes that specialise in file I/O: FileReader and FileWriter.与字节流一样,有专门用于文件 I/O 的字符流类:FileReader 和 FileWriter。

Classes under this hierarchy are used for reading and writing characters from file, which are text files.此层次结构下的类用于从文件(文本文件)中读取和写入字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java中并行stream的实用用例是什么? - What are pragmatic use cases of parallel stream in Java? 使用 java stream 查找字符串中每个字符的计数 - use java stream to find count of each character in a string 在Java中,以字符形式“构建”和使用字符串的最快方法是什么? - In Java, what's the fastest way to “build” and use a string, character by character? Java中的字符流和字节流有什么区别?C中的字符串和字节与字节有什么区别? - What's the difference Between Character Stream and Byte Stream in Java and Char vs Byte in C? 什么是预先打包的字符流类? - what are prepackaged character stream classes? Java 中的字节流与字符流 - Byte Stream vs Character Stream in Java Java:多线程字符流解码 - Java: multithreaded character stream decoding Java中的null(输入/输出)流API的用例是什么? - What is the use case for null(Input/Output)Stream API in Java? 对于BufferedReader,Java默认使用什么编码/字符集? - What encoding/character set does Java use per default for BufferedReader? Excel电子表格中的字符编码(以及用于解码它的Java字符集) - Character encoding in Excel spreadsheet (and what Java charset to use to decode it)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM