Java IO字节流读取文件学习记录

本文详细介绍了Java中两种文件读取方式:基本的FileInputStream读取和高效的FileInputStream与BufferedInputStream结合使用。通过示例代码展示了如何利用缓冲提高读取效率,并提供了异常处理和资源关闭的方法。
摘要由CSDN通过智能技术生成
package com.kj.test;

import cn.hutool.core.io.IoUtil;

import java.io.*;

/**
 * Java IO字节流读取文件总结
 * 本文对java IO流的读取文件的方式进行比较全面的总结,一个是基本的读取方式,另一个是高效的读取方式。
 */
public class FileInputTest {
    public static void main(String[] args) {
        /**
         * 基本的读取方式:使用FileInputStream
         *
         * 文件读取FileInputStream的使用
         */
        //需要读取的文件,参数是文件的路径名加文件名
        File file = new File("D:/java2.txt");
        if (file.isFile()) {
            // 以字节流方法读取文件,
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);
                // 设置一个每次加载信息的容器,指定大小为1024
                byte[] buf = new byte[1024];
                // 定义一个StringBuffer用来存放字符串
                StringBuffer buffer = new StringBuffer();
                // 开始读取数据
                int len = 0; // 每次读取到的数据长度
                while ((len = fis.read()) != -1) { //  len值为-1时,表示没有数据了
                    // append方法往buffer对象中添加数据
                    buffer.append(new String(buf, 0, len, "utf-8"));
                }
                // 输出字符串
                System.out.println(buffer.length());
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                IoUtil.close(fis);
            }
        } else {
            System.out.println("文件不存在");
        }

        /**
         * 高效的读取方式:FileInputStream和BufferInputStream一起使用
         *
         * java中BufferedInputStream类相比InputStream类,提高了输入效率,增加了输入缓冲区的功能。
         *
         * InputStream流是指将字节序列从外设或外存传递到应用程序的流。
         *
         * BufferedInputStream流是指读取数据时,数据首先保存进入缓冲区,其后的操作直接在缓冲区中完成
         */

        // 定义一个输入流对象
        FileInputStream fileInputStream = null;

        // 定义一个存放输入流的缓冲对象
        BufferedInputStream bis = null;

        // 定义一个输出流,相当于StringBuffer(),会根据读取数据的大小,调整byte数组的长度
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try {
            // 把文件路径和文件名称作为参数传给FileInputStream
            fileInputStream = new FileInputStream("D:/java2.txt");

            // 把文件读取流对象传递给缓存读取流对象
            bis = new BufferedInputStream(fileInputStream);

            // 获得缓存读取流开始的位置
            int len = bis.read();
            System.out.println("len = " + len);

            // 定义一个容器来盛放数据
            byte[] buf = new byte[1024];

            while ((len = bis.read(buf)) != -1) {
                // 如果有数据的话,就把数据添加到输出流
                // 这里也可以直接用StringBuffer的append方法接收
                baos.write(buf, 0, len);
            }

            // 把文件输出流的数据,放到字节数组中
            byte[] byteArray = baos.toByteArray();

            // 打印输出
            System.out.println(new String(byteArray, "gbk"));

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 关闭所有流,先判空,再按后开先关的原则关闭
                if (null != baos) {
                    baos.close();
                }
                if (null != bis) {
                    bis.close();
                }
                if (null != fileInputStream) {
                    fileInputStream.close();
                }
                // 也可以使用这个hutool的IoUtil.close方法,里面封装了判断和关流方法,代码看起来要简洁很多
                //IoUtil.close(baos);
                IoUtil.close(bis);
                IoUtil.close(fileInputStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值