Java IO 流核心概念与应用实践
学习目标与重点提示
学习目标:掌握 Java IO 流的核心概念,包括 IO 流的分类、常用类的使用方法,学会在实际开发中处理文件和网络数据。
Java IO 流是处理输入输出数据的机制,涵盖字节流与字符流的分类、常用类(InputStream、OutputStream、Reader、Writer)的使用、文件操作(File 类)、缓冲流与转换流的应用以及对象序列化与反序列化。文章通过代码示例演示了文件读写、网络数据处理及实际开发中的场景,帮助开发者掌握 IO 流的核心概念与实践技巧。

学习目标:掌握 Java IO 流的核心概念,包括 IO 流的分类、常用类的使用方法,学会在实际开发中处理文件和网络数据。
重点:IO 流的分类(字节流与字符流)、字节流的常用类(InputStream 与 OutputStream)、字符流的常用类(Reader 与 Writer)、文件操作(File 类的使用)、缓冲流与转换流的应用、对象序列化与反序列化。
Java IO 流是用于处理输入输出数据的机制。
定义:IO 流是用于处理输入输出数据的机制。
作用:
结论:IO 流是用于处理输入输出数据的机制,作用是读取文件内容、写入文件内容、处理网络数据和处理输入输出设备数据。
定义:IO 流的分类是指 Java 中 IO 流类的分类方法。
分类:
结论:IO 流的分类包括字节流与字符流、输入流与输出流。
字节流是处理字节数据的流。
定义:InputStream 是字节输入流的根接口。
常用子类:
示例:
import java.io.FileInputStream;
import java.io.IOException;
public class InputStreamExample {
public static void main(String[] args) {
FileInputStream fis = null;
try {
// 创建 FileInputStream 对象
fis = new FileInputStream("test.txt");
// 读取字节数据
int data = 0;
while ((data = fis.read()) != -1) {
System.out.print((char) data);
}
} catch (IOException e) {
System.out.println("读取文件异常:" + e.getMessage());
} finally {
// 关闭流
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
System.out.println("关闭流异常:" + e.getMessage());
}
}
}
}
}
输出结果:
Hello, World!
结论:InputStream 是字节输入流的根接口,常用子类包括 FileInputStream、BufferedInputStream、DataInputStream。
定义:OutputStream 是字节输出流的根接口。
常用子类:
示例:
import java.io.FileOutputStream;
import java.io.IOException;
public class OutputStreamExample {
public static void main(String[] args) {
FileOutputStream fos = null;
try {
// 创建 FileOutputStream 对象
fos = new FileOutputStream("test.txt");
// 写入字节数据
String content = "Hello, World!";
byte[] bytes = content.getBytes();
fos.write(bytes);
System.out.println("文件写入成功!");
} catch (IOException e) {
System.out.println("写入文件异常:" + e.getMessage());
} finally {
// 关闭流
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
System.out.println("关闭流异常:" + e.getMessage());
}
}
}
}
}
输出结果:
文件写入成功!
结论:OutputStream 是字节输出流的根接口,常用子类包括 FileOutputStream、BufferedOutputStream、DataOutputStream。
字符流是处理字符数据的流。
定义:Reader 是字符输入流的根接口。
常用子类:
示例:
import java.io.FileReader;
import java.io.IOException;
public class ReaderExample {
public static void main(String[] args) {
FileReader fr = null;
try {
// 创建 FileReader 对象
fr = new FileReader("test.txt");
// 读取字符数据
int data = 0;
while ((data = fr.read()) != -1) {
System.out.print((char) data);
}
} catch (IOException e) {
System.out.println("读取文件异常:" + e.getMessage());
} finally {
// 关闭流
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
System.out.println("关闭流异常:" + e.getMessage());
}
}
}
}
}
输出结果:
Hello, World!
结论:Reader 是字符输入流的根接口,常用子类包括 FileReader、BufferedReader、InputStreamReader。
定义:Writer 是字符输出流的根接口。
常用子类:
示例:
import java.io.FileWriter;
import java.io.IOException;
public class WriterExample {
public static void main(String[] args) {
FileWriter fw = null;
try {
// 创建 FileWriter 对象
fw = new FileWriter("test.txt");
// 写入字符数据
String content = "Hello, World!";
fw.write(content);
System.out.println("文件写入成功!");
} catch (IOException e) {
System.out.println("写入文件异常:" + e.getMessage());
} finally {
// 关闭流
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
System.out.println("关闭流异常:" + e.getMessage());
}
}
}
}
}
输出结果:
文件写入成功!
结论:Writer 是字符输出流的根接口,常用子类包括 FileWriter、BufferedWriter、OutputStreamWriter。
File 类是 Java 中用于处理文件和目录的类。
定义:File 类是 Java 中用于处理文件和目录的类。
常用方法:
示例:
import java.io.File;
import java.io.IOException;
public class FileExample {
public static void main(String[] args) {
// 创建 File 对象
File file = new File("test.txt");
// 判断文件是否存在
if (file.exists()) {
System.out.println("文件存在");
} else {
System.out.println("文件不存在");
// 创建新文件
try {
file.createNewFile();
System.out.println("文件创建成功");
} catch (IOException e) {
System.out.println("文件创建失败:" + e.getMessage());
}
}
// 判断是否是文件
if (file.isFile()) {
System.out.println("是文件");
} else {
System.out.println("不是文件");
}
// 判断是否是目录
if (file.isDirectory()) {
System.out.println("是目录");
} else {
System.out.println("不是目录");
}
// 获取文件名
System.out.println("文件名:" + file.getName());
// 获取文件路径
System.out.println("文件路径:" + file.getAbsolutePath());
// 获取文件大小
System.out.println("文件大小:" + file.length() + "字节");
// 删除文件
// file.delete();
// System.out.println("文件删除成功");
}
}
输出结果:
文件存在 是文件 不是目录 文件名:test.txt 文件路径:C:\Users\user\Desktop\test.txt 文件大小:13 字节
结论:File 类是 Java 中用于处理文件和目录的类,常用方法包括 exists()、isFile()、isDirectory()、createNewFile()、mkdir()、list()、delete()。
缓冲流与转换流是 Java IO 流中常用的流类型。
定义:缓冲流是用于提高 IO 效率的流类型。
常用子类:
示例:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReaderExample {
public static void main(String[] args) {
BufferedReader br = null;
try {
// 创建 BufferedReader 对象
br = new BufferedReader(new FileReader("test.txt"));
// 读取字符数据
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("读取文件异常:" + e.getMessage());
} finally {
// 关闭流
if (br != null) {
try {
br.close();
} catch (IOException e) {
System.out.println("关闭流异常:" + e.getMessage());
}
}
}
}
}
输出结果:
Hello, World!
结论:缓冲流是用于提高 IO 效率的流类型,常用子类包括 BufferedInputStream、BufferedOutputStream、BufferedReader、BufferedWriter。
定义:转换流是用于在字节流和字符流之间转换的流类型。
常用子类:
示例:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputStreamReaderExample {
public static void main(String[] args) {
InputStreamReader isr = null;
try {
// 创建 InputStreamReader 对象
isr = new InputStreamReader(new FileInputStream("test.txt"), "UTF-8");
// 读取字符数据
int data = 0;
while ((data = isr.read()) != -1) {
System.out.print((char) data);
}
} catch (IOException e) {
System.out.println("读取文件异常:" + e.getMessage());
} finally {
// 关闭流
if (isr != null) {
try {
isr.close();
} catch (IOException e) {
System.out.println("关闭流异常:" + e.getMessage());
}
}
}
}
}
输出结果:
Hello, World!
结论:转换流是用于在字节流和字符流之间转换的流类型,常用子类包括 InputStreamReader、OutputStreamWriter。
对象序列化与反序列化是 Java IO 流中用于将对象转换为字节流和将字节流转换为对象的机制。
定义:对象序列化是指将对象转换为字节流的过程。
语法:
public class 类名 implements Serializable {
// 类体
}
示例:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
// 可序列化的类
public class User implements Serializable {
private String name;
private int age;
private String email;
public User(String name, int age, String email) {
this.name = name;
this.age = age;
this.email = email;
}
public String getName() { return name; }
public int getAge() { return age; }
public String getEmail() { return email; }
}
// 测试类
public class ObjectOutputStreamExample {
public static void main(String[] args) {
ObjectOutputStream oos = null;
try {
// 创建 ObjectOutputStream 对象
oos = new ObjectOutputStream(new FileOutputStream("user.dat"));
// 创建 User 对象
User user = new User("张三", 25, "[email protected]");
// 序列化对象
oos.writeObject(user);
System.out.println("对象序列化成功!");
} catch (IOException e) {
System.out.println("对象序列化异常:" + e.getMessage());
} finally {
// 关闭流
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
System.out.println("关闭流异常:" + e.getMessage());
}
}
}
}
}
输出结果:
对象序列化成功!
结论:对象序列化是指将对象转换为字节流的过程,需要实现 Serializable 接口。
定义:对象反序列化是指将字节流转换为对象的过程。
语法:
public class 类名 implements Serializable {
// 类体
}
示例:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
// 可序列化的类
public class User implements Serializable {
private String name;
private int age;
private String email;
public User(String name, int age, String email) {
this.name = name;
this.age = age;
this.email = email;
}
public String getName() { return name; }
public int getAge() { return age; }
public String getEmail() { return email; }
}
// 测试类
public class ObjectInputStreamExample {
public static void main(String[] args) {
ObjectInputStream ois = null;
try {
// 创建 ObjectInputStream 对象
ois = new ObjectInputStream(new FileInputStream("user.dat"));
// 反序列化对象
User user = (User) ois.readObject();
// 输出用户信息
System.out.println("用户信息:");
System.out.println("姓名:" + user.getName());
System.out.println("年龄:" + user.getAge());
System.out.println("邮箱:" + user.getEmail());
} catch (IOException e) {
System.out.println("对象反序列化异常:" + e.getMessage());
} catch (ClassNotFoundException e) {
System.out.println("类未找到异常:" + e.getMessage());
} finally {
// 关闭流
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
System.out.println("关闭流异常:" + e.getMessage());
}
}
}
}
}
输出结果:
用户信息: 姓名:张三 年龄:25 邮箱:zhangsan@example.com
结论:对象反序列化是指将字节流转换为对象的过程,需要实现 Serializable 接口。
在实际开发中,IO 流的应用场景非常广泛,如:
示例:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
// 产品类
public class Product {
private String productId;
private String productName;
private double price;
public Product(String productId, String productName, double price) {
this.productId = productId;
this.productName = productName;
this.price = price;
}
public String getProductId() { return productId; }
public String getProductName() { return productName; }
public double getPrice() { return price; }
}
// 测试类
public class IOStreamApplication {
public static void main(String[] args) {
BufferedReader br = null;
List<Product> products = new ArrayList<>();
try {
// 创建 BufferedReader 对象
br = new BufferedReader(new FileReader("products.txt"));
// 读取文件内容
String line = null;
while ((line = br.readLine()) != null) {
// 分割字符串
String[] parts = line.split(",");
String productId = parts[0];
String productName = parts[1];
double price = Double.parseDouble(parts[2]);
// 创建 Product 对象
Product product = new Product(productId, productName, price);
products.add(product);
}
// 输出产品信息
System.out.println("产品信息:");
for (Product product : products) {
System.out.println("产品 ID:" + product.getProductId());
System.out.println("产品名称:" + product.getProductName());
System.out.println("价格:" + product.getPrice());
System.out.println("------------------------");
}
} catch (IOException e) {
System.out.println("读取文件异常:" + e.getMessage());
} finally {
// 关闭流
if (br != null) {
try {
br.close();
} catch (IOException e) {
System.out.println("关闭流异常:" + e.getMessage());
}
}
}
}
}
输出结果:
产品信息: 产品 ID:P001 产品名称:手机 价格:1000.0 ------------------------ 产品 ID:P002 产品名称:电脑 价格:2000.0 ------------------------ 产品 ID:P003 产品名称:平板 价格:1500.0 ------------------------
结论:在实际开发中,IO 流的应用场景非常广泛,需要根据实际问题选择合适的 IO 流类型。
本章我们学习了 Java IO 流的核心概念与应用实践,包括 IO 流的分类、常用类的使用方法,学会了在实际开发中处理文件和网络数据。其中,IO 流的分类(字节流与字符流)、字节流的常用类(InputStream 与 OutputStream)、字符流的常用类(Reader 与 Writer)、文件操作(File 类的使用)、缓冲流与转换流的应用、对象序列化与反序列化是本章的重点内容。从下一章开始,我们将学习 Java 的多线程、集合框架等内容。

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
暂无推荐文章,稍后可再来查看。
查找任何按下的键的javascript键代码、代码、位置和修饰符。 在线工具,Keycode 信息在线工具,online
JavaScript 字符串转义/反转义;Java 风格 \uXXXX(Native2Ascii)编码与解码。 在线工具,Escape 与 Native 编解码在线工具,online
使用 Prettier 在浏览器内格式化 JavaScript 或 HTML 片段。 在线工具,JavaScript / HTML 格式化在线工具,online
Terser 压缩、变量名混淆,或 javascript-obfuscator 高强度混淆(体积会增大)。 在线工具,JavaScript 压缩与混淆在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online