Java-IO流

Java IO流用于处理设备间的数据传输,按功能可分为字节流、字符流,按方向分为输入/输出流。以下是核心内容:
 
一、IO流体系结构
 
1. 基础分类
 
- 按数据单位:
- 字节流(处理二进制数据,如图片、视频):基类为 InputStream (输入)和 OutputStream (输出)。
- 字符流(处理文本数据,自动处理编码):基类为 Reader (输入)和 Writer (输出)。
- 按功能:
- 节点流(直接操作数据源,如文件、内存):如 FileInputStream 、 FileWriter 。
- 处理流(包装节点流,增强功能):如 BufferedInputStream (缓冲)、 ObjectInputStream (对象序列化)。
 
二、常用字节流
 
1. 文件字节流(节点流)
 
-  FileInputStream / FileOutputStream :读写文件二进制数据。
// 读取文件(字节数组方式)
try (FileInputStream fis = new FileInputStream("src/data.jpg")) {
    byte[] buffer = new byte[1024];
    int len;
    while ((len = fis.read(buffer)) != -1) {
        // 处理读取的字节
    }
} catch (IOException e) {
    e.printStackTrace();
}
 
 
2. 缓冲字节流(处理流)
 
-  BufferedInputStream / BufferedOutputStream :通过缓冲区减少IO次数,提升效率。
// 带缓冲的文件复制
try (FileInputStream fis = new FileInputStream("src/source.jpg");
     BufferedInputStream bis = new BufferedInputStream(fis);
     FileOutputStream fos = new FileOutputStream("dst/copy.jpg");
     BufferedOutputStream bos = new BufferedOutputStream(fos)) {
    byte[] buffer = new byte[1024];
    int len;
    while ((len = bis.read(buffer)) != -1) {
        bos.write(buffer, 0, len);
    }
}
 
 
3. 数据流(处理流)
 
-  DataInputStream / DataOutputStream :读写基本数据类型(如 int 、 String )。
// 写入数据
try (DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.dat"))) {
    dos.writeInt(100);
    dos.writeUTF("Hello");
}
// 读取数据
try (DataInputStream dis = new DataInputStream(new FileInputStream("data.dat"))) {
    int num = dis.readInt();
    String str = dis.readUTF();
}
 
 
三、常用字符流
 
1. 文件字符流(节点流)
 
-  FileReader / FileWriter :读写文本文件(默认使用系统编码)。
// 写入文本
try (FileWriter fw = new FileWriter("src/info.txt")) {
    fw.write("Hello, Java IO!");
    fw.append("\n第二行文本");
}
 
 
2. 缓冲字符流(处理流)
 
-  BufferedReader / BufferedWriter :带缓冲,支持按行读写( readLine() )。
// 按行读取文本
try (BufferedReader br = new BufferedReader(new FileReader("src/content.txt"))) {
    String line;
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
}
 
 
3. 转换流(处理流)
 
-  InputStreamReader / OutputStreamWriter :将字节流转换为字符流,指定编码(如 UTF-8 )。
// 指定UTF-8编码读取文件
try (BufferedReader br = new BufferedReader(
        new InputStreamReader(new FileInputStream("src/utf8.txt"), "UTF-8"))) {
    // 读取内容...
}
 
 
四、特殊IO流
 
1. 对象流(序列化)
 
-  ObjectInputStream / ObjectOutputStream :读写对象(需实现 Serializable 接口)。
// 序列化对象
class User implements Serializable {
    private String name;
    private int age;
    // 构造、getter/setter略
}

// 写入对象
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("user.dat"))) {
    User user = new User("张三", 25);
    oos.writeObject(user);
}

// 读取对象
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("user.dat"))) {
    User user = (User) ois.readObject();
}
 
 
2. 内存流
 
-  ByteArrayInputStream / ByteArrayOutputStream :在内存中读写数据(如临时缓存)。
 
五、Java 7+新特性:try-with-resources
 
自动关闭实现 AutoCloseable 接口的资源,避免手动关闭:
 
// 同时关闭多个流
try (FileInputStream fis = new FileInputStream("in.txt");
     FileOutputStream fos = new FileOutputStream("out.txt")) {
    // 操作...
} catch (IOException e) {
    e.printStackTrace();
}
 
 
六、IO流最佳实践
 
- 1. 使用缓冲流:减少底层IO次数(如 BufferedReader 比 FileReader 效率高)。
- 2. 及时关闭资源:优先用 try-with-resources ,或在 finally 中关闭。
- 3. 处理编码问题:读取文本时指定编码(如 UTF-8 ),避免乱码。
- 4. 分块读取:大文件避免一次性读入内存(如用1024字节缓冲区)。
- 5. 异常处理:IO操作需捕获 IOException ,并记录错误信息。
 
总结
 
Java IO流通过分层设计(节点流+处理流)提供灵活的数据处理能力。开发中需根据场景选择合适的流类型(字节流/字符流),结合缓冲、编码转换等处理流优化性能,同时利用 try-with-resources 确保资源正确释放。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值