Java连载95-流的继承结构图、FileInputStream举例

一、使用UML表示InputStream和OutputStream的继承结构图

二、使用UML表示Reader和Writer的继承结构图

三、FileInputStream举例

package com.bjpowernode.java_learning;

import java.io.*;

​

public class D95_1_FileInputStreamAnalysis {

  public static void main(String[] args) {

    try {

      //1.要读取某一个文件,先于这个文件创建一个“输入流”

      //文件路径

      String filePath = "temp1.txt";//相对路径

      FileInputStream fls = new FileInputStream(filePath);

      int i1 = fls.read();//以字节的方式读取

      System.out.println(i1);

      int i2 = fls.read();

      int i3 = fls.read();

      System.out.println(i2);

      System.out.println(i3);

     

      //2.开始读文件

    }catch (FileNotFoundException e) {

      e.printStackTrace();

    }catch (Exception e2) {

      e2.printStackTrace();

    }finally {

      //为了保证流一定会被释放,所以在finally语句块中执行

      try {

        fls.close();

      }catch (Exception e3) {

        e3.printStackTrace();

      }

    }

  }

}

对象是按照一个字节一个字节读取的,最后如果读取到最后一个字符的时候,在下一个就没有了,read()会返回-1值​

四、源码:

D95_1_FileInputStreamAnalysis.java

https://github.com/ruigege66/Java/blob/master/D95_1_FileInputStreamAnalysis.java

2.CSDN:https://blog.csdn.net/weixin_44630050

3.博客园:https://www.cnblogs.com/ruigege0000/

4.欢迎关注微信公众号:傅里叶变换,个人公众号,仅用于学习交流,后台回复”礼包“,获取大数据学习资料

 

原文地址:https://www.cnblogs.com/ruigege0000/p/12459936.html