java InputStream抽象类介绍

发布于:2024-07-16 ⋅ 阅读:(143) ⋅ 点赞:(0)

java.io.InputStream 是 Java 输入输出(I/O)类库中的一个抽象类,它是所有字节输入流类的超类。输入流用于读取数据(通常是从文件、网络连接或其他数据源),读取的内容为字节数据。下面是对 InputStream 类的详细介绍。

InputStream 类概述

InputStream 是一个抽象类,定义了字节输入流的基本方法。它的子类实现了具体的数据读取逻辑,比如从文件、网络、字节数组等读取数据。

主要方法

InputStream 类提供了一些基本的方法供子类实现和调用。以下是一些常用的方法:

int read()
public abstract int read() throws IOException;

读取单个字节,返回读取的字节(0-255 范围内的整数),如果到达流的末尾,则返回 -1。

int read(byte[] b)

读取一定数量的字节,并将它们存储到字节数组 b 中。返回读取的字节数,如果到达流的末尾则返回 -1。

public int read(byte[] b) throws IOException;
int read(byte[] b, int off, int len)

读取最多 len 个字节,并将它们存储到字节数组 b 中,从偏移量 off 开始。返回读取的字节数,如果到达流的末尾则返回 -1。

public int read(byte[] b, int off, int len) throws IOException;
long skip(long n)

跳过和丢弃输入流中的 n 个字节。

public long skip(long n) throws IOException;
int available()

返回可以不受阻塞地从该输入流读取(或跳过)的剩余字节数。

public int available() throws IOException;
void close()

关闭输入流并释放与该流关联的所有系统资源。

public void close() throws IOException;
void mark(int readlimit)

标记当前流中的位置,以便稍后使用 reset() 方法重新定位到该位置。

public synchronized void mark(int readlimit);
void reset()

将流重新定位到最后一次调用 mark() 方法时的位置。

public synchronized void reset() throws IOException;
boolean markSupported()

测试输入流是否支持 mark 和 reset 方法。

public boolean markSupported();

常用子类

以下是 InputStream 类的常见子类:

FileInputStream 用于读取文件的输入流。
FileInputStream fileInputStream = new FileInputStream("file.txt");
ByteArrayInputStream 用于读取字节数组的输入流。
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
DataInputStream 允许应用程序以机器无关方式从基础输入流中读取基本 Java 数据类型。
DataInputStream dataInputStream = new DataInputStream(new FileInputStream("file.txt"));
ObjectInputStream 用于反序列化从流中读取的 Java 对象。
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("file.txt"));
PipedInputStream 实现了管道输入流,用于在不同线程之间通信。
PipedInputStream pipedInputStream = new PipedInputStream();
BufferedInputStream 提供了缓冲功能的输入流,通常用于提高读取效率。
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("file.txt"));

​​​​​​​

示例代码

以下是一个简单的示例代码,演示如何使用 FileInputStream 读取文件:

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamExample {
    public static void main(String[] args) {
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream("example.txt");
            int data;
            while ((data = inputStream.read()) != -1) {
                System.out.print((char) data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

在上述示例中,FileInputStream 被用来读取文件 example.txt,并将读取的字节逐个打印到控制台。

总结

InputStream 是 Java I/O 类库中的一个重要抽象类,它定义了读取字节数据的基本操作。通过继承 InputStream类并实现其抽象方法,可以处理多种不同类型的数据源。HTSJDK库中的设计思路也是通过类似的抽象类和接口,实现对多种数据格式的支持。