【java高级】java.io.InputStream

一.概述

java.io.InputStream是java标准库提供的最基本的输入流,它是一个抽象类,是所有输入流的超类。其中有一个read()方法,会读取输入流的下一个字节,并返回字节表示的int值(0~255)。如果已读到末尾,返回-1表示不能继续读取了。

二.FileInputStream

InputStream的一个子类。
我们通过查看FileInputStream的官方文档,我们主要看第三个构造函数。
在这里插入图片描述
主要看一下read()函数。
在这里插入图片描述

package com.sdnu.project1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author Beyong
 * @Description FileInputStream
 * @date 2021/05/23 17:52
 */

public class Demo1 {
    public static void main(String[] args) {
        FileInputStream fileDate = null;
        try{
            fileDate = new FileInputStream("d:\\test.txt");
            System.out.println(fileDate.read());
            System.out.println(fileDate.read());
            System.out.println(fileDate.read());
            System.out.println(fileDate.read());
            System.out.println(fileDate.read());
            System.out.println(fileDate.read());
            System.out.println(fileDate.read());
            System.out.println(fileDate.read());
            System.out.println(fileDate.read());
            System.out.println(fileDate.read());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if( fileDate != null){
                try{
                    fileDate.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

在这里插入图片描述

在这里插入图片描述

改进版,循环读

package com.sdnu.project1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author Beyong
 * @Description FileInputStream
 * @date 2021/05/23 17:52
 */

public class Demo1 {
    public static void main(String[] args) {
        FileInputStream fileDate = null;
        /* 改进*/
        try{
            fileDate = new FileInputStream("d:\\test.txt");
            int temp = 0;
            while((temp = fileDate.read()) != -1){
                System.out.println(temp);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if( fileDate != null){
                try{
                    fileDate.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

总结:FileInputStream用于从文件读取数据

三.缓冲区管理

(1)开辟缓冲区读取

上面我们虽然实现了文件以流的方式读取,但是每一次一个字节效率低,于是我们想到开辟一个缓冲区,一次读取多个字节。

txt文件:
在这里插入图片描述

程序:

package com.sdnu.project1;
import java.io.*;
/**
 * @author Beyong
 * @Description buffer
 * @date 2021/05/24 13:46
 */

public class Demo2 {
    public static void main(String[] args) {
        FileInputStream file = null;
        try{
            file = new FileInputStream("D:\\test.txt");
            byte[] buffer = new byte[4];
            int readCount;
            /*第一次*/
            readCount = file.read(buffer);
            System.out.println(readCount);
            /*第二次*/
            readCount = file.read(buffer);
            System.out.println(buffer);
            /*第三次*/
            readCount = file.read(buffer);
            System.out.println(buffer);

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

结果:
在这里插入图片描述

我们通过byte[] buffer = new byte[4];在内存当中开辟了4个字节大小空间作为我们的缓冲区,然后每一次读取4个字节。
(注意,file.read(buffer)返回的结果是读取的字节数)
在这里插入图片描述
在这里插入图片描述

(2)转换字符串

输出改为:

System.out.println(new String(buffer));

我们通过new String()可以将byte转换成字符串对象。

在这里插入图片描述
三次读取内存变化:
在这里插入图片描述
(3)最终版

package com.sdnu.project1;
import java.io.*;
/**
 * @author Beyong
 * @Description buffer
 * @date 2021/05/24 13:46
 */

public class Demo2 {
    public static void main(String[] args) {
        FileInputStream file = null;
        try{
            file = new FileInputStream("D:\\test.txt");
            byte[] buffer = new byte[4];
            while(true){
                int readCount = file.read(buffer);
                if(readCount == -1){
                    break;
                }
                System.out.println(new String(buffer, 0, readCount));
            }
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

在这里插入图片描述
有多少读多少。

改进:

package com.sdnu.project1;
import java.io.*;
/**
 * @author Beyong
 * @Description buffer
 * @date 2021/05/24 13:46
 */

public class Demo2 {
    public static void main(String[] args) {
        FileInputStream file = null;
        try{
            file = new FileInputStream("D:\\test.txt");
            System.out.println("总字节数为 :" + file.available());
            byte[] buffer = new byte[file.available()];
            int readCount = file.read(buffer);
            System.out.println("读进去的字节数为 :" + readCount);
            System.out.println(new String(buffer));
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

在这里插入图片描述

作者:Beyong    
出处:Beyong博客
github地址:https://github.com/beyong2019


本博客中未标明转载的文章归作者Beyong有,欢迎转载,但未经作者同意必须保留此段声明,且在文章明显位置给出原文连接,否则保留追究法律责任的权利。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值