java IO流之FileInputStream 字节读入流 构造方法 实例化

FileInputStream

  • FileInputStream 从文件系统中的某个文件中获得输入字节。哪些文件可用取决于主机环境。

  • 用于读取诸如图像数据之类的原始字节流。如果文件是文本文件,那么就不要使用字节流读取了,建议使用字符流FileReader

  • 在UTF-8格式文件中,英文字符 底层实际占用1个字节,中文字符 底层实际占用3个字节。

构造方法

  • FileInputStream(File file) 通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定。

  • FileInputStream(FileDescriptor fdObj) 通过使用文件描述符 fdObj 创建一个 FileInputStream,该文件描述符表示到文件系统中某个实际文件的现有连接。

  • FileInputStream(String name) 通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。

方法

Modifier and TypeMethodDescription
intavailable()返回下一次对此输入流调用的方法可以不受阻塞地从此输入流读取(或跳过)的估计剩余字节数。
voidclose()关闭此文件输入流并释放与此流有关的所有系统资源。
protected voidfinalize()确保在不再引用文件输入流时调用其 close 方法。
FileChannelgetChannel()返回与此文件输入流有关的唯一 FileChannel 对象。
FileDescriptorgetFD()返回表示到文件系统中实际文件的连接的 FileDescriptor 对象,该文件系统正被此 FileInputStream 使用。
intread()从此输入流中读取一个数据字节。 read方法底层做了处理,让返回的数据都是“正数”,到文件结尾返回1
intread(byte[] b)从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。返回读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回 -1。
intread(byte[] b, int off, int len)从此输入流中将最多 len 个字节的数据读入一个 byte 数组b中。b - 存储读取数据的缓冲区。off - 目标数组 b 中的起始偏移量。len - 读取的最大字节数。
longskip(long n)从输入流中跳过并丢弃 n 个字节的数据。

从类 java.io.InputStream继承的方法 mark, markSupported, reset

从类 java.lang.Object继承的方法 clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |

分步实例

  1. 有一个源文件,创建一个File类的对象

    File f = new File("IOStream/src/testfile/test.txt");
    
  2. 将一个字节流这个管 怼 到 源文件上:创建一个FileInputStream类的对象

    FileInputStream fis = new FileInputStream(f);
    
  3. 读取文件

    1. 逐字节读取文件

      //3.1逐字节读取文件
      int count = 0;//定义一个计数器,用来计读入的字节的个数
      int n = fis.read();
      while(n!=-1){
      	count++;
      	System.out.print((char)n);
      	n = fis.read();
      }
      System.out.println("\ncount="+count);
      
      1. 利用缓冲数组读取

        //3.2利用缓冲数组读取
        byte[] b = new byte[1024*5];
        int len = fis.read(b);//len指的就是读取的数组中的有效长度
        while(len!=-1){
            for(int i = 0;i<len;i++){
                System.out.print((char)b[i]);
            }
            len = fis.read(b);
        }
        
  4. 关闭流

    fis.close();
    

读入程序

搭配try-catch-finally异常处理的完整程序

public class InSF {
    public static void main(String[] args){
        //功能:利用字节流将文件中内容读到程序中来:
        //1.有一个源文件,创建一个File类的对象
        File f = new File("IOStream/src/testfile/test.txt");
        //2.将一个字节流这个管 怼  到 源文件上:创建一个FileInputStream类的对象
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(f);
            //3.1逐字节读取文件
            int count = 0;//定义一个计数器,用来计读入的字节的个数
            int n = fis.read();
            while(n!=-1){
                count++;
                System.out.print((char)n);
                n = fis.read();
            }
            System.out.println("\ncount="+count);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //4.关闭流:
            try {
                assert fis != null;
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

try-with-resource

public class InSF {
    public static void main(String[] args){
        //功能:利用字节流将文件中内容读到程序中来:
        //1.有一个源文件,创建一个File类的对象
        File f = new File("IOStream/src/testfile/test.txt");
        //2.将一个字节流这个管 怼  到 源文件上:创建一个FileInputStream类的对象
        try (
                FileInputStream fis = new FileInputStream(f);
                ){
            //3.1逐字节读取文件
            int count = 0;//定义一个计数器,用来计读入的字节的个数
            int n = fis.read();
            while(n!=-1){
                count++;
                System.out.print((char)n);
                n = fis.read();
            }
            System.out.println("\ncount="+count);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("找不到文件");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
posted @ 2021-08-14 16:22  SKPrimin  阅读(158)  评论(0编辑  收藏  举报