Java IO streams explain (a) - a brief introduction

Files are streamed in a program to transfer. So we have to transfer files using Java to use Java IO streams.


1, the concept and flow action

Flow: objects representative of any data source or the ability to output data receiving terminal is capable of receiving object data <Thinking in Java>

Essence stream: data transmission, data transmission characteristics of the various types of flow abstract, convenient data manipulation more intuitive.

Action: establishing a conveying path for the data source and destination


2, the model used by Java IO

Java's IO model design is very good, it uses Decorator (Decorator) mode (this blog is a detailed description of what the decorator pattern portal ), divided by function Stream, you can dynamically assemble these Stream, in order to get the functionality you need .

       For example, you need a buffered file input stream, and it should be used in combination FileInputStream BufferedInputStream.


3, the IO stream classification

    ①, the data is divided into different flows: the input and output streams.

          Output: output contents (memory) program to disk, optical disk storage devices

          Input: read the external data (data disk, an optical disk storage device) to the program (memory) in

image


    ②, according to the different types of data processing are divided into: byte stream and character stream.

          Byte stream: reading and writing binary file and can be used in any type of document.

          Character stream: can be used to read and write text files.


     The difference between a byte stream and a character stream

     The origin character stream: Java is the use of characters in the Unicode standard, a 16-bit character, i.e. a character using two bytes to represent. To this end, JAVA introduced streaming characters. Because different data encoding, thereby to stream objects with the efficient operation of the character. When in fact essentially based on the read byte stream, to check specified code table.

     Difference between the two: (1 = 2-byte characters, a byte (byte) = 8 bit ( 'bit), a character length is two bytes)

  1. Different reader unit: bytes in the byte stream, a character stream in characters, the characters according to the code table mapping, a plurality of bytes may be read.
  2. Different processed: byte stream can handle all types of data (such as images, avi, etc.), the character stream can handle the type of character data.
  3. Different cache: byte stream at the time of the operation itself is not used in the buffer, the file itself is directly operated; and character stream when in operation after that will be used at the buffer, the buffer is coming through Action file.

     Summary: preferred byte stream. Because all the files on the hard drives are in the form of bytes transmitted or stored, including pictures and other content. Character will be formed only in memory, so in development, byte stream, widely used. Unless the plain text data (such as TXT files), it is preferred to use a character stream, in addition to use the byte stream.


     ③, divided according to different functions: node flow and processing flow.

     Node Stream: read and write data from or to a specific place (node). Such as FileInputStream, FileReader.

     Process Flow: Is the package and connected to an existing flow, the flow through the function call encapsulated for data reading and writing. As BufferedReader. Constructor processing flow is always to bring one other stream object as an argument. A stream object after several other packaging stream, called a link flow.

     Node stream is a stream acts directly on the document, it can be understood as a conduit, file transfer in the pipeline.

     Process flow stream is already acting on the basis of the node, the node is out of the pipe flow (which may be a multilayer) package, Its purpose is to flow within the conduit may be faster transmission.

image


4, four basic types of abstract streams, all streams inherit these four.

Input stream Output stream
Byte stream InputStream OutputStream
Character stream Reader Writer


  The figure is the overall architecture diagram Java IO streams behind the blog will explain in detail the use of these streams:

image

   When you see the above picture when the force would be very ignorant, because so many classes in the use of the byte stream input and output is selected or choose a character stream input and output it? In fact, not difficult.

  1. If the data you want to read the disk in the CD program, then select the input stream, the output stream and vice versa.
  2. If you want to transfer audio and video data is, it certainly is a stream of bytes. If the individual is a plain text file (such as TXT), then the character stream. In fact, all the data can be used byte stream, but the level of efficiency issue.
  3. The above two are determined, and can select an appropriate node of the stream, and then depending on whether your needs require additional functions (such as whether the flow needs to be converted, high flow, etc.).

image


5、IO流常用到的五类一接口

      Java IO流中所有的接口和类都放在java.io这个包下。其中最重要的就是5个类和一个接口。5个类指的是File、OutputStream、InputStream、Writer、Reader;一个接口指的是Serializable。如掌握了这些IO的核心使用方法,那么就能把Java IO流玩弄于股掌之中,将它放在手中任意盘它006DDA0B006DDA0B006DDA0B

     它们的详细介绍如下:

     ①. File(文件特征与管理):File类是对文件系统中文件以及文件夹进行封装的对象,可以通过对象的思想来操作文件和文件夹。 File类保存文件或目录的各种元数据信息,包括文件名、文件长度、最后修改时间、是否可读、获取当前文件的路径名,判断指定文件是否存在、获得当前目录中的文件列表,创建、删除文件和目录等方法。 

     ②. InputStream(二进制格式操作):抽象类,基于字节的输入操作,是所有输入流的父类。定义了所有输入流都具有的共同特征。

     ③. OutputStream(二进制格式操作):抽象类。基于字节的输出操作。是所有输出流的父类。定义了所有输出流都具有的共同特征。

     ④. Reader(文件格式操作):抽象类,基于字符的输入操作。

     ⑤. Writer(文件格式操作):抽象类,基于字符的输出操作。

     ⑥. RandomAccessFile(随机文件操作):一个独立的类,直接继承至Object.它的功能丰富,可以从文件的任意位置进行存取(输入输出)操作。

     ⑦. Serializable(序列化操作):是一个空接口,为对象提供标准的序列化与反序列化操作。


6、Java IO流对象介绍

    ①、字节输入流InputStream

image

    根据上图介绍各个类的作用:

  1. InputStream:字节输入流基类,是所有的字节输入流的父类,它是一个抽象类。
  2. FileInputSream:文件输入流。它通常用于对文件进行读取操作。
  3. FilterInputStream :过滤流。作用是为基础流提供一些额外的功能。装饰者模式中处于装饰者,具体的装饰者都要继承它,所以在该类的子类下都是用来装饰别的流的,也就是处理类。
  4. BufferedInputStream:缓冲流。对处理流进行装饰,增强,内部会有一个缓存区,用来存放字节,每次都是将缓存区存满然后发送,而不是一个字节或两个字节这样发送。效率更高。
  5. DataInputStream:数据输入流。它是用来装饰其它输入流,它“允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型”。
  6. PushbakInputStream:回退输入流。java中读取数据的方式是顺序读取,如果某个数据不需要读取,需要程序处理。PushBackInputStream就可以将某些不需要的数据回退到缓冲中。
  7. ObjectInputStream:对象输入流。用来提供对“基本数据或对象”的持久存储。通俗点讲,也就是能直接传输对象(反序列化中使用)。
  8. PipedInputStream:管道字节输入流。它和PipedOutputStream一起使用,能实现多线程间的管道通信。
  9. SequenceInputStream:合并输入流。依次将多个源合并成一个源。
  10. StringBufferInputStream:字符相关流。已经过时不多说。
  11. ByteArrayInputStream:字节数组输入流,该类的功能就是从字节数组(byte[])中进行以字节为单位的读取,也就是将资源文件都以字节的形式存入到该类中的字节数组中去,我们拿也是从这个字节数组中拿。



    ②、字节输出流OutputStream

image

    根据上图介绍各个类的作用:

  1. OutputStream:字节输出流基类,是所有的字节输出流的父类,它是一个抽象类。
  2. FileOutputStream:文件输出流。该类实现了一个输出流,将数据输出到文件。
  3. FilterOutputStream :过滤流。用来封装其它的输出流,并为它们提供额外的功能(序列化中使用)。它主要包括BufferedOutputStream, DataOutputStream和PrintStream。
  4. BufferedOutputStream:缓冲输出流。给输出流提供缓冲功能。
  5. DataOutputStream:是用来装饰其它输出流,将DataOutputStream和DataInputStream输入流配合使用,“允许应用程序以与机器无关方式从底层输入流中读写基本 Java 数据类型”。
  6. PrintStream:是用来装饰其它输出流。它能为其他输出流添加了功能,使它们能够方便地打印各种数据值表示形式。
  7. ObjectOutputStream:对象输出流。该类将实现了序列化的对象序列化后写入指定地方。 
  8. PipedOutputStream:管道字节输出流。它和PipedInputStream一起使用,能实现多线程间的管道通信,是管道的发送端。
  9. ByteArrayOutputStream:字节数组输出流。该类实现了一个输出流,其数据被写入由byte数组充当的缓冲区,缓冲区会随着数据的不断写入而自动增长。



    ③、字符输入流Reader

image

    根据上图介绍各个类的作用:

  1. Reader:是所有的输入字符流的父类,它是一个抽象类。
  2. CharReader、StringReader 是两种基本的介质流,它们分别将Char 数组、String中读取数据。
  3. PipedReader 是从与其它线程共用的管道中读取数据。
  4. BufferedReader是一个装饰器,它和其子类LineNumberReader负责装饰其它Reader对象。
  5. InputStreamReader:是一个连接字节流和字符流的桥梁,它将字节流转变为字符流。FileReader 可以说是一个达到此功能、常用的工具类,在其源代码中明显使用了将FileInputStream 转变为Reader 的方法。我们可以从这个类中得到一定的技巧。Reader 中各个类的用途和使用方法基本和InputStream 中的类使用一致。后面会有Reader 与InputStream 的对应关系。
  6. FilterReader 是所有自定义具体装饰流的父类,其子类PushbackReader 对Reader对象进行装饰,会增加一个行号。



    ④、字符输出流Writer

image

    根据上图介绍各个类的作用:

  1. Writer:是所有的输出字符流的父类,它是一个抽象类。
  2. CharArrayWriter、StringWriter 是两种基本的介质流,它们分别向Char 数组、String 中写入数据。PipedWriter 是向与其它线程共用的管道中写入数据,
  3. BufferedWriter 是一个装饰器为Writer 提供缓冲功能。
  4. PrintWriter 和PrintStream 极其类似,功能和使用也非常相似。
  5. OutputStreamWriter:是OutputStream 到Writer 转换的桥梁,它的子类FileWriter 其实就是一个实现此功能的具体类(具体可以研究一SourceCode)。功能和使用和OutputStream 极其类似。


7、字符流与字节流转换

     转换流的作用:文本文件在硬盘中以字节流的形式存储时,通过InputStreamReader读取后转化为字符流给程序处理,程序处理的字符流通过OutputStreamWriter转换为字节流保存。

     转换流的特点:其是字符流和字节流之间的桥梁

          可对读取到的字节数据经过指定编码转换成字符

          可对读取到的字符数据经过指定编码转换成字节

     何时使用转换流?

          When a switching operation between the byte and character;

          Dataflow operations require encoding or decoding.

     Concrete objects reflect:

          InputStreamReader (InputStream in): the character stream input to the byte stream.

          OutputStreamWriter (OutStream out): the character stream output stream of bytes.

    These two characters in the stream object is a member of the system, they have converted the role itself is a character stream, so the construction time need to pass the byte stream object come.



Reference article:

https://blog.csdn.net/sinat_37064286/article/details/86537354

Guess you like

Origin www.cnblogs.com/tanghaorong/p/12322376.html