字节缓冲流

发布时间:2026/6/26 2:07:12
字节缓冲流 # 竞赛IO文件复制作业博客 ## 任务来源 幻灯片主题竞赛题-homework to blog 知识点分类 1. 文本文件复制字符缓冲流最常用 2. 任意文件复制字节缓冲流万能复制 ## 一、两种缓冲流对比 1. 字符缓冲流 适用txt、java、md 纯文本优势按行读取、中文不乱码考试首选。 2. 字节缓冲流 适用图片、视频、压缩包等全部文件优势二进制万能复制无格式限制。 ## 二、核心代码 ### 1. 字符缓冲流复制文本 java import java.io.*; public class CharCopy { public static void main(String[] args) throws IOException { BufferedReader br new BufferedReader(new FileReader(src.txt)); BufferedWriter bw new BufferedWriter(new FileWriter(dest.txt)); String line; while((line br.readLine()) ! null){ bw.write(line); bw.newLine(); } bw.close(); br.close(); } }2. 字节缓冲流万能复制java运行import java.io.*; public class ByteCopy { public static void main(String[] args) throws IOException { BufferedInputStream bis new BufferedInputStream(new FileInputStream(src)); BufferedOutputStream bos new BufferedOutputStream(new FileOutputStream(dest)); byte[] buf new byte[8192]; int len; while((len bis.read(buf)) ! -1){ bos.write(buf,0,len); } bos.close(); bis.close(); } }三、竞赛答题要点纯文本用字符缓冲流二进制文件必须用字节缓冲流缓冲流自带缓冲区读写效率高于基础节点流使用完成后必须关闭流遵循先开后关原则。作业总结本次 homework to blog 竞赛作业区分两类 IO 缓冲流场景提供考试标准模板代码覆盖文件复制全部考点。