【Java SE】Java IO 类常用方法大全:从字节流到 NIO 的核心 API 汇总

简介

本文汇总 Java IO 核心类及方法,含字节流、字符流、缓冲流、转换流、对象流、随机访问文件及 NIO 的 Buffer、Channel 等。详细列出各类型构造方法与读写、关闭等操作,助开发者快速查阅,提升文件处理效率。

文章目录

InputStream

  • int read(): 读取单个字节的数据
  • int read(byte[] b): 读取字节到数组中,返回实际读取的字节数
  • int read(byte[] b, int off, int len): 从偏移量off开始读取最多len个字节到数组b中
  • long skip(long n): 跳过并丢弃输入流中的n个字节
  • int available(): 返回可以读取的字节数
  • void close(): 关闭输入流并释放资源
  • void mark(int readlimit): 标记当前位置
  • void reset(): 重置到上次标记的位置
  • boolean markSupported(): 判断是否支持标记操作

FileInputStream

  • FileInputStream(String name): 通过文件名创建文件输入流
  • FileInputStream(File file): 通过File对象创建文件输入流
  • int read(): 读取单个字节
  • int read(byte[] b): 读取字节到数组
  • long skip(long n): 跳过n个字节
  • int available(): 返回可读取的字节数
  • void close(): 关闭流

OutputStream

  • void write(int b): 写入单个字节
  • void write(byte[] b): 写入整个字节数组
  • void write(byte[] b, int off, int len): 写入字节数组的一部分
  • void flush(): 刷新输出流,强制写出所有缓冲的数据
  • void close(): 关闭输出流并释放资源

FileOutputStream

  • FileOutputStream(String name): 通过文件名创建文件输出流
  • FileOutputStream(String name, boolean append): 创建文件输出流,可选择是否追加
  • FileOutputStream(File file): 通过File对象创建文件输出流
  • void write(int b): 写入单个字节
  • void write(byte[] b): 写入字节数组
  • void flush(): 刷新输出流
  • void close(): 关闭流

Reader

  • int read(): 读取单个字符
  • int read(char[] cbuf): 读取字符到数组中
  • int read(char[] cbuf, int off, int len): 读取字符到数组的指定位置
  • long skip(long n): 跳过n个字符
  • boolean ready(): 判断是否准备好读取
  • void close(): 关闭读取器
  • void mark(int readAheadLimit): 标记当前位置
  • void reset(): 重置到上次标记的位置
  • boolean markSupported(): 判断是否支持标记

FileReader

  • FileReader(String fileName): 通过文件名创建文件读取器
  • FileReader(File file): 通过File对象创建文件读取器
  • int read(): 读取单个字符
  • int read(char[] cbuf): 读取字符到数组
  • long skip(long n): 跳过n个字符
  • boolean ready(): 判断是否准备好读取
  • void close(): 关闭读取器

Writer

  • void write(int c): 写入单个字符
  • void write(char[] cbuf): 写入字符数组
  • void write(char[] cbuf, int off, int len): 写入字符数组的一部分
  • void write(String str): 写入字符串
  • void write(String str, int off, int len): 写入字符串的一部分
  • Writer append(CharSequence csq): 添加字符序列
  • Writer append(CharSequence csq, int start, int end): 添加字符序列的一部分
  • Writer append(char c): 添加单个字符
  • void flush(): 刷新输出
  • void close(): 关闭写入器

FileWriter

  • FileWriter(String fileName): 通过文件名创建文件写入器
  • FileWriter(String fileName, boolean append): 创建文件写入器,可选择是否追加
  • FileWriter(File file): 通过File对象创建文件写入器
  • void write(int c): 写入单个字符
  • void write(char[] cbuf): 写入字符数组
  • void write(String str): 写入字符串
  • void flush(): 刷新输出
  • void close(): 关闭写入器

BufferedInputStream

  • BufferedInputStream(InputStream in): 创建带默认缓冲区大小的缓冲输入流
  • BufferedInputStream(InputStream in, int size): 创建指定缓冲区大小的缓冲输入流
  • int read(): 读取单个字节
  • int read(byte[] b, int off, int len): 读取字节到数组
  • long skip(long n): 跳过n个字节
  • int available(): 返回可读取的字节数
  • void close(): 关闭流
  • void mark(int readlimit): 标记当前位置
  • void reset(): 重置到上次标记的位置
  • boolean markSupported(): 判断是否支持标记

BufferedOutputStream

  • BufferedOutputStream(OutputStream out): 创建带默认缓冲区大小的缓冲输出流
  • BufferedOutputStream(OutputStream out, int size): 创建指定缓冲区大小的缓冲输出流
  • void write(int b): 写入单个字节
  • void write(byte[] b, int off, int len): 写入字节数组的一部分
  • void flush(): 刷新输出流
  • void close(): 关闭流

BufferedReader

  • BufferedReader(Reader in): 创建带默认缓冲区大小的缓冲读取器
  • BufferedReader(Reader in, int sz): 创建指定缓冲区大小的缓冲读取器
  • String readLine(): 读取一行文本
  • int read(): 读取单个字符
  • int read(char[] cbuf, int off, int len): 读取字符到数组
  • long skip(long n): 跳过n个字符
  • boolean ready(): 判断是否准备好读取
  • void close(): 关闭读取器
  • void mark(int readAheadLimit): 标记当前位置
  • void reset(): 重置到上次标记的位置

BufferedWriter

  • BufferedWriter(Writer out): 创建带默认缓冲区大小的缓冲写入器
  • BufferedWriter(Writer out, int sz): 创建指定缓冲区大小的缓冲写入器
  • void write(int c): 写入单个字符
  • void write(char[] cbuf, int off, int len): 写入字符数组的一部分
  • void write(String s, int off, int len): 写入字符串的一部分
  • void newLine(): 写入行分隔符
  • void flush(): 刷新输出
  • void close(): 关闭写入器

InputStreamReader

  • InputStreamReader(InputStream in): 创建使用默认字符集的转换输入流
  • InputStreamReader(InputStream in, String charsetName): 创建使用指定字符集的转换输入流
  • InputStreamReader(InputStream in, Charset cs): 创建使用指定字符集的转换输入流
  • String getEncoding(): 获取当前使用的字符编码
  • int read(): 读取单个字符
  • int read(char[] cbuf, int offset, int length): 读取字符到数组
  • boolean ready(): 判断是否准备好读取
  • void close(): 关闭读取器
  • long skip(long n): 跳过n个字符

OutputStreamWriter

  • OutputStreamWriter(OutputStream out): 创建使用默认字符集的转换输出流
  • OutputStreamWriter(OutputStream out, String charsetName): 创建使用指定字符集的转换输出流
  • OutputStreamWriter(OutputStream out, Charset cs): 创建使用指定字符集的转换输出流
  • String getEncoding(): 获取当前使用的字符编码
  • void write(int c): 写入单个字符
  • void write(char[] cbuf, int off, int len): 写入字符数组的一部分
  • void write(String str, int off, int len): 写入字符串的一部分
  • void flush(): 刷新输出
  • void close(): 关闭写入器
  • boolean ready(): 判断是否准备好写入

ObjectInputStream

  • ObjectInputStream(InputStream in): 创建对象输入流
  • Object readObject(): 读取对象
  • boolean readBoolean(): 读取布尔值
  • byte readByte(): 读取字节
  • char readChar(): 读取字符
  • short readShort(): 读取短整型
  • int readInt(): 读取整型
  • long readLong(): 读取长整型
  • float readFloat(): 读取浮点型
  • double readDouble(): 读取双精度浮点型
  • void defaultReadObject(): 读取默认序列化的对象
  • void readFields(): 读取对象的持久字段
  • int available(): 返回可读取的字节数
  • void close(): 关闭流

ObjectOutputStream

  • ObjectOutputStream(OutputStream out): 创建对象输出流
  • void writeObject(Object obj): 写入对象
  • void writeBoolean(boolean val): 写入布尔值
  • void writeByte(int val): 写入字节
  • void writeChar(int val): 写入字符
  • void writeShort(int val): 写入短整型
  • void writeInt(int val): 写入整型
  • void writeLong(long val): 写入长整型
  • void writeFloat(float val): 写入浮点型
  • void writeDouble(double val): 写入双精度浮点型
  • void defaultWriteObject(): 写入对象的默认序列化数据
  • void writeFields(): 写入对象的持久字段
  • void flush(): 刷新输出流
  • void close(): 关闭流
  • void reset(): 重置对象缓存
  • void writeClassDescriptor(ObjectStreamClass desc): 写入类描述符

RandomAccessFile

  • RandomAccessFile(String name, String mode): 创建随机访问文件
  • RandomAccessFile(File file, String mode): 通过File对象创建随机访问文件
  • void seek(long pos): 设置文件指针位置
  • long getFilePointer(): 获取当前文件指针位置
  • long length(): 获取文件长度
  • void setLength(long newLength): 设置文件长度
  • int read(): 读取单个字节
  • int read(byte[] b): 读取字节到数组
  • int read(byte[] b, int off, int len): 读取字节到数组的指定位置
  • boolean readBoolean(): 读取布尔值
  • byte readByte(): 读取字节
  • char readChar(): 读取字符
  • short readShort(): 读取短整型
  • int readInt(): 读取整型
  • long readLong(): 读取长整型
  • float readFloat(): 读取浮点型
  • double readDouble(): 读取双精度浮点型
  • String readLine(): 读取一行文本
  • void write(int b): 写入单个字节
  • void write(byte[] b): 写入字节数组
  • void write(byte[] b, int off, int len): 写入字节数组的一部分
  • void writeBoolean(boolean v): 写入布尔值
  • void writeByte(int v): 写入字节
  • void writeChar(int v): 写入字符
  • void writeShort(int v): 写入短整型
  • void writeInt(int v): 写入整型
  • void writeLong(long v): 写入长整型
  • void writeFloat(float v): 写入浮点型
  • void writeDouble(double v): 写入双精度浮点型
  • void writeBytes(String s): 写入字符串的字节序列
  • void writeChars(String s): 写入字符串的字符序列
  • void writeUTF(String str): 以UTF-8编码写入字符串
  • String readUTF(): 读取UTF-8编码的字符串
  • int skipBytes(int n): 跳过n个字节
  • void close(): 关闭文件

Buffer

  • int capacity(): 返回缓冲区的容量
  • int position(): 返回缓冲区的位置
  • Buffer position(int newPosition): 设置缓冲区的位置
  • int limit(): 返回缓冲区的限制
  • Buffer limit(int newLimit): 设置缓冲区的限制
  • Buffer clear(): 清空缓冲区,准备新的通道读取或相对放置操作
  • Buffer flip(): 反转此缓冲区
  • Buffer rewind(): 重绕此缓冲区
  • int remaining(): 返回当前位置与限制之间的元素数
  • boolean hasRemaining(): 判断当前位置和限制之间是否有元素
  • Buffer mark(): 在此缓冲区的当前位置设置标记
  • Buffer reset(): 重置此缓冲区的位置到标记位置
  • int compareTo(Buffer that): 将此缓冲区与另一个缓冲区进行比较
  • boolean equals(Object ob): 判断此缓冲区是否等于另一个对象
  • int hashCode(): 返回此缓冲区的哈希码

Channel

  • int read(ByteBuffer dst): 从通道读取数据到缓冲区
  • long read(ByteBuffer[] dsts): 分散读取,从通道读取到多个缓冲区
  • long read(ByteBuffer[] dsts, int offset, int length): 从指定偏移量开始分散读取
  • int write(ByteBuffer src): 从缓冲区写入数据到通道
  • long write(ByteBuffer[] srcs): 聚集写入,从多个缓冲区写入到通道
  • long write(ByteBuffer[] srcs, int offset, int length): 从指定偏移量开始聚集写入
  • long transferFrom(ReadableByteChannel src, long position, long count): 从源通道传输数据
  • long transferTo(long position, long count, WritableByteChannel target): 传输数据到目标通道
  • void close(): 关闭通道
  • boolean isOpen(): 判断通道是否打开

Path

  • Path getFileName(): 返回此路径的文件名组件
  • Path getParent(): 返回此路径的父路径
  • Path getRoot(): 返回此路径的根组件
  • int getNameCount(): 返回此路径中的名称元素数量
  • Path getName(int index): 返回此路径的指定名称元素
  • Path subpath(int beginIndex, int endIndex): 返回此路径的子路径
  • boolean isAbsolute(): 判断此路径是否为绝对路径
  • Path toAbsolutePath(): 返回此路径的绝对路径
  • Path normalize(): 返回此路径的规范化路径
  • Path resolve(Path other): 解析此路径与给定路径
  • Path resolve(String other): 解析此路径与给定路径字符串
  • boolean startsWith(Path other): 判断此路径是否以给定路径开始

Files

  • static long size(Path path): 返回文件大小
  • static boolean exists(Path path): 判断文件是否存在
  • static boolean isRegularFile(Path path): 判断是否为普通文件
  • static boolean isDirectory(Path path): 判断是否为目录
  • static Path createFile(Path path): 创建文件
  • static Path createDirectory(Path dir): 创建目录
  • static Path createDirectories(Path dir): 创建多级目录
  • static Path delete(Path path): 删除文件
  • static boolean deleteIfExists(Path path): 如果存在则删除文件
  • static Path copy(Path source, Path target): 复制文件
  • static Path move(Path source, Path target): 移动文件
  • static List<String> readAllLines(Path path): 读取文件的所有行
  • static byte[] readAllBytes(Path path): 读取文件的所有字节
  • static Path write(Path path, byte[] bytes): 写入字节到文件
  • static Path write(Path path, Iterable<? extends CharSequence> lines): 写入行到文件
相关推荐
路边草随风3 小时前
java实现发布flink k8s application模式作业
java·大数据·flink·kubernetes
编程修仙3 小时前
第二篇 SpringBoot项目启动流程
java·spring boot·后端
张较瘦_3 小时前
Springboot3 | MyBatis-Plus 多表查询极简实践:宠物管理系统场景落地
java·mybatis·宠物
weixin_307779133 小时前
Jenkins Pipeline Graph View插件:可视化流水线的核心工具
运维·开发语言·架构·jenkins
秋月的私语3 小时前
c#字符串Split与CSV解析中的引号处理
服务器·开发语言·c#
济南壹软网络科技有限公司3 小时前
基于 UniApp + PHP 的壹软V4Max旗舰盲盒商城系统技术实现方案
开发语言·uni-app·php·盲盒源码
fanruitian3 小时前
springboot4 swagger3
java·springboot·swagger3·springboot4
⑩-3 小时前
JVM-内存模型
java·jvm
小猪快跑爱摄影3 小时前
【AutoCad 2025】【C#】零基础教程(一)——Rider 构建 HELLO 插件
开发语言·c#