【学习笔记】Java IO流之字节流和字符流知识点整理

本文介绍了Java IO流的分类,包括输入流、输出流、字节流和字符流,并详细阐述了节点流与处理流的概念。重点讲解了Java 7之前的try-catch-finally和Java 7后新增的try-with-resource两种流操作方式,通过文件复制的例子展示了两种写法的差异。文章强调了技术进步如何简化编程步骤,鼓励读者深入学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I/O流分类

(1)按数据流的流向不同分为:输入流,输出流

输入/输出是相对于java应用程序即内存而言:

从文件等 → java程序内存:输入流。
从java程序内容 → 文件等:输出流。

(2)按操作数据单位不同分为:

字节流:按字节,8 bit,适合二进制文件)
字符流:按字符,每个字符多少字节与编码格式有关,适合文本文件

Java IO流技术
最顶端的抽象基类:

输入流:字节流-InputStream,字符流-Reader
输出流:字节流-OutputStream,字符流-Writer

(3)按流的角色不同分为:

节点流(与数据源如文件、网络、java内存等直接关联的流),
处理流/包装流(使用到设计模式:装饰器模式)

Java IO流的写法

JAVA7之前的写法try-catch-finally

在try-catch-finally块外声明变量并初始化为null,try块内初始化、读写流,在finally块中关闭流声明输入输出流变量

try {
    // 初始化流
    // 流读写
} catch (Exception e) {
    // 异常处理
} finally {
    // 关闭流
}

文件复制示例:将一个文件中的内容复制到另一个文件中,并使用缓存流提高读写效率。

FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
    fileInputStream = new FileInputStream("D:\\test.txt");
    bufferedInputStream = new BufferedInputStream(fileInputStream);
    fileOutputStream = new FileOutputStream("D:\\test1.txt");
    bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
    int readLength = 0;
    byte[] buffer = new byte[1024];
    while ((readLength = bufferedInputStream.read(buffer)) != -1) {
        //不能直接使用write(buffer)
        bufferedOutputStream.write(buffer, 0, readLength); 
    }
    // flush方法是否调用均可,很多输出流的close方法中也会调用flush方法
    bufferedOutputStream.flush(); 
} catch (Exception e) {
    // 异常处理
    e.printStackTrace();
} finally {
    // 关闭流,流的close方法需要try-catch,不然一个流关闭失败可能会影响剩下流的关闭,一般关闭时的异常不做特殊处理
    if(bufferedInputStream != null) {
        try {
            bufferedInputStream.close();
        } catch (IOException e) {
            // 异常处理
            e.printStackTrace();
        }
    }
    if(bufferedOutputStream != null) {
        try {
            bufferedOutputStream.close();
        } catch (IOException e) {
            // 异常处理
            e.printStackTrace();
        }
    }
}

JAVA7后新增的try-with-resource写法

在try块中定义输入输出流对象,则会在流的读写操作结束之后由系统自动关闭流

try(输入输出流定义) {
    // 流读写
} catch (Exeception e) {
    // 异常处理
}

上述文件复制示例重写:

try(FileInputStream fileInputStream = new FileInputStream("D:\\test.txt");
    BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
    FileOutputStream fileOutputStream = new FileOutputStream("D:\\test1.txt");
    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream)) {
    int readLength = 0;
    byte[] buffer = new byte[1024];
    while ((readLength = bufferedInputStream.read(buffer)) != -1) {
        bufferedOutputStream.write(buffer, 0, readLength);
    }
    bufferedOutputStream.flush();
} catch (Exception e) {
    e.printStackTrace();
}

看完之后是不是感觉代码少了很多,技术在升级,也在简化实现步骤。一起卷起来,学起来。

评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小冷coding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值