The Java byte input stream [the InputStream]

First, the input stream of bytes

  java.io.InputStream abstract class is a superclass of all the input stream of bytes, byte information can be read into memory.

  It defines the basic functionality common method of input stream of bytes.

  Common methods:

public void close (): Close this input stream and releases any system resources associated with this stream. 
public abstract int read (): Reads the next byte of data from the input stream. 
public int read (byte [] b ): number of bytes read from the input stream, and stores them into a byte array b.

  Note : close method, when the flow of the operation is complete, you must call this method to release system resources.

   Subclass of InputStream
    

  Dark flow node, the processing flow of light.

Two, FileInputStream class

  java.io.FileInputStream class file input stream is read from the data file, used to read into memory. 

  1, construction method

FileInputStream (File file): Creates a FileInputStream by opening a connection to the actual file, the file named by the File object file system file. 
FileInputStream (String name): Creates a FileInputStream by opening a connection to the actual file, the file named by the pathname of the file system name.

    Parameters : read file data source

      Path to the file: String name

      File file: file object

     The role of the constructor :

      ① creates a FileInputStream objects

      ② will FileInputStream objects in the specified constructor file to be read

    Principle read data (HDD -> Memory)

      java program -> JVM -> OS -> OS method of reading data -> Read File

    Byte input stream using the steps [ Important ]:

      ① create a FileInputStream object constructor to bind the data source to be read

      ② use read FileInputStream object to read the file

      ③ release resources

  2, reads the byte data

    (1) reads bytes : Read method, each time a byte of data can be read, to enhance the int type, to the end of the file is read, returns - 1 , demonstrates the use of the code:

. 1   public  static  void main (String [] args) throws IOException {
 2          // 1. Create FileInputStream object constructor bound to read the data source 
. 3          FileInputStream FIS = new new FileInputStream ( "E: \\ C.txt" );
 4          // 2. use read FileInputStream object, to read the file
 . 5          // int read () to read a byte of the file and returns, to the end of the file is read or -1 
. 6              / * 
7              found repeating the above read file is a process that can be used to optimize loop
 8              does not know how many bytes of the file, while loop
 9              the while loop end conditions, when read -1
 10  
. 11              Boolean expression (len = ! fis.read ()) = -. 1
 12 is                 1.fis.read (): read a byte
 13 is                  2.len = fis.read (): read the bytes assigned to the variable len
 14                  3. (len = fis.read ()) = -! 1: determining whether the variable is not equal to len -1
 15           * / 
16          int len = 0; // record the read byte 
. 17          the while - ((len = fis.read ()) = 1! {)
 18 is              the System.out .print (len); // ABC 
. 19          }
 20 is  
21 is          // 3. release resources 
22 is          fis.close ();
 23 is      }

 

    (2) using the byte array read : Read (byte [] b) , each read b length bytes to the array, return the number of bytes read is valid, to the end of the reading, the return - 1

    int read(byte[] b) 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。

    明确两件事情:

      a. 方法的参数byte[]的作用?

        起到缓冲作用,存储每次读取到的多个字节,数组的长度一把定义为1024(1kb)或者1024的整数倍

      b.方法的返回值int是什么?

        每次读取的有效字节个数

      Demo :

 1  public static void main(String[] args) throws IOException {
 2         //创建FileInputStream对象,构造方法中绑定要读取的数据源
 3         FileInputStream fis = new FileInputStream("E:\\b.txt");
 4         //使用FileInputStream对象中的方法read读取文件
 5         //int read(byte[] b) 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。
 6          /*
 7             发现以上读取时一个重复的过程,可以使用循环优化
 8             不知道文件中有多少字节,所以使用while循环
 9             while循环结束的条件,读取到-1结束
10          */
11         byte[] bytes = new byte[1024];//存储读取到的多个字节
12         int len = 0; //记录每次读取的有效字节个数
13         while((len = fis.read(bytes))!=-1){
14             //String(byte[] bytes, int offset, int length) 把字节数组的一部分转换为字符串 offset:数组的开始索引 length:转换的字节个数
15             System.out.println(new String(bytes,0,len));
16         }
17 
18         //释放资源
19         fis.close();
20     }

 

  Tips:使用数组读取,每次读取多个字节,减少了系统间的IO操作次数,从而提高了读写的效率,建议开发中使用。

  字节流读取文件的原理:

 

 

 

三、复制文件案例

  复制文件原理图解

 

   文件复制的步骤

    1. 创建一个字节输入流对象,构造方法中绑定要读取的数据源

    2. 创建一个字节输出流对象,构造方法中绑定要写入的目的地

    3. 使用字节输入流对象中的方法read读取文件

    4. 使用字节输出流中的方法write,把读取到的字节写入到目的地的文件中

    5. 释放资源

  代码实现

 1 public static void main(String[] args) throws IOException {
 2         //1.创建一个字节输入流对象,构造方法中绑定要读取的数据源
 3         FileInputStream fis = new FileInputStream("c:\\1.jpg");
 4         //2.创建一个字节输出流对象,构造方法中绑定要写入的目的地
 5         FileOutputStream fos = new FileOutputStream("d:\\1.jpg");
 6         //一次读取一个字节写入一个字节的方式
 7         //3.使用字节输入流对象中的方法read读取文件
 8         /*int len = 0;
 9         while((len = fis.read())!=-1){
10             //4.使用字节输出流中的方法write,把读取到的字节写入到目的地的文件中
11             fos.write(len);
12         }*/
13 
14         //使用数组缓冲读取多个字节,写入多个字节
15         byte[] bytes = new byte[1024];
16         //3.使用字节输入流对象中的方法read读取文件
17         int len = 0;//每次读取的有效字节个数
18         while((len = fis.read(bytes))!=-1){
19             //4.使用字节输出流中的方法write,把读取到的字节写入到目的地的文件中
20             fos.write(bytes,0,len);
21         }
22 
23         //5.释放资源(先关写的,后关闭读的;如果写完了,肯定读取完毕了)
24         fos.close();
25         fis.close();
26     }

 

   注意:流的关闭原则,先开后关,后开先关。

Guess you like

Origin www.cnblogs.com/niujifei/p/11487490.html