zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Java中的字符流与字节流

2023-09-14 09:13:18 时间

Java中的字符流与字节流

0.引言

在应用中,经常要完成字符的一段文本输出或读取,用字节流可以吗?为什么需要字符流

1.计算机中的一切最终都是二进制的字节形式存在。

  • 01.写入时,首先读到的是字符,需要得到其对应的字节,然后将字节写入到输出流。
  • step 02.读取时,首先读到的是字节,可是我们要把它显示为字符,我们需要将字节转换成字符。
    例如:对于 “中国”这两个字符。在写入时,需要将其得到相应的字节,然后写入到输出流;在读取时,将相应的字节流得到字符流,然后通过字符流输出。

由于这样的需求很广泛,所以又专门提供了一个对字节流的包装类——字符流。字符流直接接受字符串,它内部将字符串转换成字节,再写入底层设备。

2.字符向字节转换时,要注意编码的问题

因为字符串转成字节数组,其实是转成该字符的某种编码的字节形式。
同样,由字节流转换成字符流,也有编码形式的存在。【因为你需要将这些字节编码成相应的字符】

3.字节流【InputStream OutputStream】

  • InputStream 类
/**
 This abstract class is the superclass of all classes representing an input stream of bytes.
 这个抽象类是所有字节输入流的超类
 
 Applications that need to define a subclass of InputStream must always provide a method that returns the next byte of input.
 应用程序需要定义一个InputStream的子类,这个子类必须提供一个方法,该方法用于返回输入的下一个字节。
 
 @author  Arthur van Hoff
 @see     java.io.BufferedInputStream
 @see     java.io.ByteArrayInputStream
 @see     java.io.DataInputStream
 @see     java.io.FilterInputStream
 @see     java.io.InputStream#read()
 @see     java.io.OutputStream
 @see     java.io.PushbackInputStream
 @since   JDK1.0
 */
public abstract class InputStream implements Closeable {...}

4.字符流【InputStreamReader OutputStreamWriter】

  • FileReader
public class FileReader
extends InputStreamReader

Convenience class for reading character files. The constructors of this class assume that the
default character encoding and the default byte-buffer size are appropriate. To specify these
values yourself, construct an InputStreamReader on a FileInputStream. 
FileReader is meant for reading streams of characters. For reading streams of raw bytes, 
consider using a FileInputStream.

Since: 
JDK1.1 
See Also: 
InputStreamReader, FileInputStream 

译注:
(1)类FileReader继承自InputStreamReader
(2)FileReader是一个读取字符文件的方便类。这个类的构造器假设默认的字符编码和默认的字节缓冲大小是合适的。如果你想自己制定这些值,构造一个InputStreamReader在一个FileInputStream
(3)FileReader是用来阅读字符流的。如果想阅读字节流,请考虑使用FileInputStream

  • InputStreamReader类
/**
 An InputStreamReader is a bridge from byte streams to character streams: It
 reads bytes and decodes them into characters using a specified 
 java.nio.charset.Charset charset.  The charset that it uses
 may be specified by name or may be given explicitly, or the platform's
 default charset may be accepted.
 一个InputStreamReader对象是一个字节流到字符流的桥梁:
 它读取字节并使用指定的java.nio.charset.Charset 字符对象将其解码为字符。
 被使用的字符可能通过name指定或者被显式指出,或者平台的默认使用字符。
 
 <p> Each invocation of one of an InputStreamReader's read() methods may
 cause one or more bytes to be read from the underlying byte-input stream.
 To enable the efficient conversion of bytes to characters, more bytes may
 be read ahead from the underlying stream than are necessary to satisfy the
 current read operation.
 
 <p> For top efficiency, consider wrapping an InputStreamReader within a
 BufferedReader.  For example:
 
 <pre>
 BufferedReader in
   = new BufferedReader(new InputStreamReader(System.in));
 </pre>
 
 @see BufferedReader
 @see InputStream
 @see java.nio.charset.Charset
 
 @author      Mark Reinhold
 @since       JDK1.1
 */

public class InputStreamReader extends Reader {...}