Java 文件操作与IO流
·
一、File 类
1.1 概念
File 类是 Java 中文件和目录路径名的抽象表示,主要用于对文件或目录进行创建、删除、遍历、信息查询等操作。 注意:File 仅表示路径和属性,不直接操作文件内容。
1.2 构造方法
File file1 = new File("路径名");
File file2 = new File("父路径", "子路径");
File file3 = new File(File parent, String child);
1.3 常用 API 方法
文件创建与目录管理
| 方法 | 说明 |
|---|---|
exists() |
判断文件或文件夹是否存在 |
createNewFile() |
创建新文件(若已存在则返回 false) |
mkdir() |
创建单级目录 |
mkdirs() |
创建多级目录 |
delete() |
删除文件或空目录 |
判断文件类型
| 方法 | 说明 |
|---|---|
isDirectory() |
是否为目录 |
isFile() |
是否为文件 |
文件信息获取
| 方法 | 说明 |
|---|---|
getName() |
获取文件名 |
getPath() |
获取路径(相对路径) |
getAbsolutePath() |
获取绝对路径 |
getParent() |
获取父路径 |
length() |
返回文件的字节数(即文件大小) |
文件筛选(过滤器)
通过函数式接口实现文件筛选:
-
FileFilter:过滤文件对象; -
FilenameFilter:过滤文件名称。
应用场景:
-
获取指定类型文件(如
.txt文件) -
查找特定目录中的文件
示例:
File dir = new File("src");
File[] files = dir.listFiles(f -> f.getName().endsWith(".java"));
二、IO 流体系概览
| 分类 | 字节流 | 字符流 |
|---|---|---|
| 输入流 | InputStream | Reader |
| 输出流 | OutputStream | Writer |
| 缓冲流 | BufferedInputStream / BufferedOutputStream | BufferedReader / BufferedWriter |
| 转换流 | — | InputStreamReader / OutputStreamWriter |
| 数据流 | DataInputStream / DataOutputStream | — |
三、字节流
3.1 InputStream(字节输入流)
作用: 以字节(8 bit)为单位读取数据,用于读取二进制文件(图片、音频、视频等)。
常用子类:
-
FileInputStream:从文件读取; -
BufferedInputStream:带缓冲区的高效流; -
ByteArrayInputStream:读取内存字节数组。
常用方法:
int read(); // 读取一个字节 int read(byte[] b); // 读取多个字节到数组中 void close(); // 关闭流
示例:
try (FileInputStream fis = new FileInputStream("data.txt")) {
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
System.out.write(buffer, 0, len);
}
}
3.2 OutputStream(字节输出流)
作用: 以字节为单位写入数据。
常用子类:
-
FileOutputStream -
BufferedOutputStream -
ByteArrayOutputStream
常用方法:
void write(int b); // 写一个字节 void write(byte[] b); // 写一个字节数组 void write(byte[] b, int off, int len); // 写部分字节 void flush(); // 刷新缓冲区
四、字符流(基于字节流的转换)
4.1 InputStreamReader(字节 → 字符)
作用: 将字节流转换为字符流,指定字符集解码。
常见用法:
InputStream in = new FileInputStream("test.txt");
InputStreamReader isr = new InputStreamReader(in, "UTF-8");
BufferedReader br = new BufferedReader(isr);
典型场景:
-
控制台输入 (
System.in) -
文件读取(配合缓冲流)
4.2 OutputStreamWriter(字符 → 字节)
作用: 将字符流转换为字节流,用于以特定编码输出文本。
常见用法:
OutputStream os = new FileOutputStream("out.txt");
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
BufferedWriter bw = new BufferedWriter(osw);
应用:
-
网络传输输出
-
以指定编码写入文本文件
五、内存流与资源加载
内存中的流对象
用于读取内存中已有的字节或字符数据:
ByteArrayInputStream bis = new ByteArrayInputStream(byteArray); ByteArrayOutputStream bos = new ByteArrayOutputStream();
获取类路径下资源
读取与类在同级目录的资源文件:
InputStream in = 类名.class.getResourceAsStream("login.txt");
六、编码集与转换
编码集(Charset)决定字节与字符的转换方式,常见有:
-
UTF-8(通用编码)
-
GBK(中文环境常用)
-
ISO-8859-1(西欧语言)
常见错误:
-
编码不统一会导致中文乱码问题。
七、总结对比
| 类型 | 单位 | 适用场景 | 常见类 |
|---|---|---|---|
| 字节流 | 1 字节(8位) | 图片、音频、视频等二进制文件 | FileInputStream / FileOutputStream |
| 字符流 | 2 字节(16位) | 文本文件、字符串数据 | FileReader / FileWriter |
| 转换流 | 字节 ↔ 字符 | 编码转换、网络传输 | InputStreamReader / OutputStreamWriter |
| 缓冲流 | — | 提高IO效率 | BufferedReader / BufferedWriter |
更多推荐


所有评论(0)