Java 614 - IO流原理和分类以及FileInputStream

Java的IO流用于处理输入输出,包括输入流、输出流,字节流、字符流等。FileInputStream作为字节输入流,用于读取文件内容。通过构造函数创建实例后,可以使用read()方法读取数据,读取完毕返回-1。为了提高效率,可以使用字节数组一次性读取多个字节。
摘要由CSDN通过智能技术生成

目录

介绍

IO流分类图

FileInputStream


介绍

Java的IO流(Input/Output Streams)是一种用于处理输入输出的机制。它提供了一种在Java程序中读取和写入数据的通用方式,不论是从文件、网络连接、管道、内存等来源读取,还是写入到这些目标之一。IO流被广泛地用于Java应用程序中。

Java的IO流根据数据流的方向和流的类型被分成了两种不同的类型:输入流和输出流。输入流用于从源读取数据,而输出流用于将数据写入到目标。

Java的IO流根据数据的单位和特点分成了四种类型:

  1. 字节流(Byte Streams):字节流用于处理二进制数据,它以字节为单位读取和写入数据。它包括两种类型的流:InputStream和OutputStream。

  2. 字符流(Character Streams):字符流用于处理文本数据,它以字符为单位读取和写入数据。它包括两种类型的流:Reader和Writer。

  3. 缓冲流(Buffered Streams):缓冲流可以提高IO性能,它通过在内存中创建缓冲区来减少IO操作的次数,从而提高了程序的效率。它包括了BufferedInputStream、BufferedOutputStream、BufferedReader和BufferedWriter。

  4. 对象流(Object Streams):对象流用于处理对象数据,它可以把一个对象转换为字节流进行读写。它包括ObjectInputStream和ObjectOutputStream。

在Java的IO流中,数据流的方向可以分为输入流和输出流。同时,数据流的类型可以分为字节流、字符流、缓冲流和对象流。这些流的组合可以为Java应用程序提供非常灵活和高效的数据读写方式。

IO流分类图

 

FileInputStream

FileInputStream是Java中一个用于读取文件内容的类,它继承自InputStream类,可以用于读取任何类型的文件数据。

下面是FileInputStream的一些知识点和使用方法:

  1. FileInputStream的构造函数

FileInputStream有两种构造函数:

  • public FileInputStream(File file):根据指定的File对象创建一个FileInputStream对象。
  • public FileInputStream(String name):根据指定的文件路径名创建一个FileInputStream对象。
  1. 读取文件数据

FileInputStream可以通过read()方法读取文件中的字节数据。read()方法返回读取到的字节的ASCII码值,如果到达文件的结尾,则返回-1。

FileInputStream也可以通过read(byte[] b)方法读取文件中的字节数组。该方法返回读取到的字节数,如果到达文件的结尾,则返回-1。

  1. 关闭文件流

在使用FileInputStream时,必须调用close()方法来关闭流,以释放文件资源。

下面是一个简单的FileInputStream的使用示例:

    @Test
    public void readFile01(){
        String filePath = "software/hello.txt";
        int readCount;
        int readData = 0;
//        创建FileInputStream对象,读取文件
        FileInputStream fileInputStream = null;
        try {
            readCount=0;
            fileInputStream = new FileInputStream(filePath);
//            读取文件返回-1表示读取完毕
            while ((readData = fileInputStream.read())!=-1){
                readCount++;
                System.out.printf("%s", (char)readData);
            };
            System.out.println();
            System.out.println("总共读取了"+readCount+"次");
        }catch (IOException e){
            e.printStackTrace();
        }finally {
//            关闭资源
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

缺点: 每个字节读取,效率较低,性能较差。

 

可以做如下改变,提高性能,一次性读取八个字节。

@Test
    public void readFile02(){
        String filePath = "software/hello.txt";
//        定义一个count去统计读取次数
        int readCount;
        int readLength = 0;
//        定义字节数组,企图一次读8个字节
        byte[] bytes = new byte[8];

//        创建FileInputStream对象,读取文件
        FileInputStream fileInputStream = null;
        try {
            readCount=0;
            fileInputStream = new FileInputStream(filePath);

//            读取文件返回-1表示读取完毕
//            如果读取正常,则返回实际读取的字节数
            while ((readLength=fileInputStream.read(bytes))!=-1){
                readCount++;
                System.out.print(new String(bytes, 0, readLength));
            };
            System.out.println();
            System.out.println("总共读取了:"+readCount+"次");
        }catch (IOException e){
            e.printStackTrace();
        }finally {
//            关闭资源
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

优点:相比之前的每个字节读取,提高了效率。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值