FileInputStreamRead详解

一、FileInputStreamReader

FileInputStreamReader (一般使用子类 FileReader) 是 Java 中的一个用于读取文件的类。该类是 InputStreamReader 的子类,它按照指定编码方式从文件中读取字符。

FileReader相对于 InputStreamReader来说查找文件不需要指定编码方式,它会按照文件的默认编码方式读取

FileInputStreamReader常用构造函数:

    public InputStreamReader(InputStream in); 
    public InputStreamReader(InputStream in, String charsetName);

第一个构造函数是通过给定的 InputStream 对象和默认字符集来创建对象。第二个构造函数是构造一个根据指定字符集来读取流的 InputStreamReader。

二、FileInputStreamRead读取文件

FileInputStreamRead 是用于从文件中读取字节流的类,用于读取字节流时,一般会结合 BufferedReader 类和 InputStreamReader 类来使用。

读取的核心方法为 read(byte[] b) 和 read(byte[] b, int off, int len)。

第一个 read 方法从输入流中读取一些字节,并将它们存储到缓冲区数组 b 中。第二个 read 方法是从流中读取 len 个字节,并将其存储在偏移量 off 的缓冲区数组 b 中。

读取文件的代码示例:

    public static void main(String[] args){
        try {
            FileInputStream fis = new FileInputStream("test.txt"); // 创建字节输入流
            byte[] data = new byte[1024]; // 创建一个 bytes 数组,用来存储读取的数据
            int len = fis.read(data); // 读取数据
            String result = new String(data, 0, len, "UTF-8"); // 解码读取到的数据(默认以 UTF-8 解码)
            System.out.println(result); // 打印结果
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

三、FileInputStreamRead用法

FileInputStreamRead 有很多用法,下面介绍几个常用的。

1. 读取文件到字节数组中

将文件读取到字节数组中,可以自定义缓冲区大小,从而提高读取效率。

    public static byte[] readFileToByteArray(File file) throws IOException {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            byte[] buffer = new byte[1024];
            int readLength;
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            while ((readLength = fis.read(buffer)) != -1) {
                bos.write(buffer, 0, readLength);
            }
            return bos.toByteArray();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    // Ignore exception.
                }
            }
        }
    }

2. 读取文件到字符缓冲区中

在文件较大且需要一次读取全部数据的情况下,可以使用 BufferedReader 来提高读取效率,同时可以通过指定字符集来解决读取中文时乱码的问题。

    public static void readFileToStringBuffer(File file) throws IOException {
        FileInputStream fis = null;
        BufferedReader reader = null;
        try {
            fis = new FileInputStream(file);
            reader = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            System.out.println(sb.toString());
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    // Ignore exception.
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    // Ignore exception.
                }
            }
        }
    }

3. 读取压缩文件

利用 Java 自带的 GZIPInputStream 类,可以读取压缩后的文件。

    public static void readCompressedFile(File file) throws IOException {
        GZIPInputStream gis = null;
        try {
            gis = new GZIPInputStream(new FileInputStream(file));
            BufferedReader reader = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } finally {
            if (gis != null) {
                try {
                    gis.close();
                } catch (IOException e) {
                    // Ignore exception.
                }
            }
        }
    }

4. 读取网络文件

通过 URL 类从网络上读取文件,通常用于获取需要下载的文件的链接,然后使用 HttpURLConnection 类下载文件。

    public static void readRemoteFile(String url) throws IOException {
        InputStream in = new URL(url).openStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        in.close();
    }

四、总结

FileInputStreamRead 是 Java 中用于读取文件的一个重要类,其使用范围广泛。其中,FileInputStreamReader 用于按指定编码方式从文件中读取字符,而 FileInputStreamRead 用于读取字节流。

本文介绍了 FileInputStreamRead 的一些常用用法,包括将文件读取到字节数组中、将文件读取到字符缓冲区中、读取压缩文件以及从网络上读取文件等用法。对于需要从文件读取数据的 Java 开发人员来说,掌握 FileInputStreamRead 是非常重要的。

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/242269.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-12 12:47
下一篇 2024-12-12 12:47

相关推荐

  • 神经网络代码详解

    神经网络作为一种人工智能技术,被广泛应用于语音识别、图像识别、自然语言处理等领域。而神经网络的模型编写,离不开代码。本文将从多个方面详细阐述神经网络模型编写的代码技术。 一、神经网…

    编程 2025-04-25
  • Linux sync详解

    一、sync概述 sync是Linux中一个非常重要的命令,它可以将文件系统缓存中的内容,强制写入磁盘中。在执行sync之前,所有的文件系统更新将不会立即写入磁盘,而是先缓存在内存…

    编程 2025-04-25
  • MPU6050工作原理详解

    一、什么是MPU6050 MPU6050是一种六轴惯性传感器,能够同时测量加速度和角速度。它由三个传感器组成:一个三轴加速度计和一个三轴陀螺仪。这个组合提供了非常精细的姿态解算,其…

    编程 2025-04-25
  • C语言贪吃蛇详解

    一、数据结构和算法 C语言贪吃蛇主要运用了以下数据结构和算法: 1. 链表 typedef struct body { int x; int y; struct body *nex…

    编程 2025-04-25
  • 详解eclipse设置

    一、安装与基础设置 1、下载eclipse并进行安装。 2、打开eclipse,选择对应的工作空间路径。 File -> Switch Workspace -> [选择…

    编程 2025-04-25
  • Python输入输出详解

    一、文件读写 Python中文件的读写操作是必不可少的基本技能之一。读写文件分别使用open()函数中的’r’和’w’参数,读取文件…

    编程 2025-04-25
  • Linux修改文件名命令详解

    在Linux系统中,修改文件名是一个很常见的操作。Linux提供了多种方式来修改文件名,这篇文章将介绍Linux修改文件名的详细操作。 一、mv命令 mv命令是Linux下的常用命…

    编程 2025-04-25
  • Java BigDecimal 精度详解

    一、基础概念 Java BigDecimal 是一个用于高精度计算的类。普通的 double 或 float 类型只能精确表示有限的数字,而对于需要高精度计算的场景,BigDeci…

    编程 2025-04-25
  • git config user.name的详解

    一、为什么要使用git config user.name? git是一个非常流行的分布式版本控制系统,很多程序员都会用到它。在使用git commit提交代码时,需要记录commi…

    编程 2025-04-25
  • nginx与apache应用开发详解

    一、概述 nginx和apache都是常见的web服务器。nginx是一个高性能的反向代理web服务器,将负载均衡和缓存集成在了一起,可以动静分离。apache是一个可扩展的web…

    编程 2025-04-25

发表回复

登录后才能评论