字节流和字符流 in Java

@0: 深入理解Java中的流(Stream)

@1:字节流:
java.io.InputStream:
public abstract class InputStream
This abstract class is the superclass of all classes representing an input stream of bytes.

java.io.OutputStream:
public abstract class OutputStream
This abstract class is the superclass of all classes representing an output stream of bytes.

InputStream是所有字节输入流的祖先,而OutputStream是所有字节输出流的祖先。

@2:字符流:
java.io.Reader:
public abstract class Reader
Abstract class for reading character streams.

java.io.Writer:
public abstract class Writer
Abstract class for writing to character streams.

Reader是所有读取字符串输入流的祖先,而Writer是所有输出字符串的祖先。

InputStream,OutputStream,Reader,writer都是抽象类。所以不能直接new。

@3:对比:

字符流使用了缓冲区,而字节流没有使用缓冲区.对于是否使用缓冲区的验证和代码示例,可以看这里.

Q:使用字节流好还是字符流好?
A:使用字节流更好.

所有的文件在硬盘或在传输时都是以字节的方式进行的,包括图片等都是按字节的方式存储的,而字符是只有在内存中才会形成,所以

在开发中,字节流使用较为广泛。

java.io中还出现了许多其他的流,主要都是为了提高性能和使用方便.

原文地址:https://www.cnblogs.com/lxw0109/p/java-byte-character-stream.html