- 一、缓冲的概念
- 二、IO流的分类
- 1.字节输入输出流
- 2.字符输入输出流
- 综合案例
一、缓冲的概念
看视频有点卡?暂停一下,加载缓冲一下。
快递:送到物流中转站,然后分批次的发。物流中转站就是缓冲的概念。
IO流的本质就是对电脑的文件进行读和写的
计算机通过CPU内存读取磁盘上面的文件数据,一次读取1字节。但是可以加上缓冲的概念
每次读取4kb,效率会变高
二、IO流的分类 1.字节输入输出流package com.qfedu.fileoutstream; import java.io.*; public class Demo2 { public static void main(String[] args) throws IOException { long start = System.currentTimeMillis(); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(("F:/aaa/1.mp4"))); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(("F:/aaa/bbb/1.mp4"))); byte[] bytes = new byte[4 * 1024]; int length; while ((length = bis.read(bytes)) != -1) { System.out.println("哈哈"); bos.write(bytes, 0, length); } bos.close(); bis.close(); long end = System.currentTimeMillis(); System.out.println(end - start);//324ms copyVideo(); } public static void copyVideo() throws IOException { long start = System.currentTimeMillis(); FileInputStream fis = new FileInputStream(("F:/aaa/1.mp4")); FileOutputStream fos = new FileOutputStream(("F:/aaa/bbb/1.mp4")); int length; while ((length = fis.read()) != -1) { System.out.println("哈哈"); fos.write(length); } fos.close(); fis.close(); long end = System.currentTimeMillis(); System.out.println(end - start);//444435ms } }2.字符输入输出流
FileReader:是一个阅读字符文件的便利类,是专门处理字符文件的,比如txt文件。
是从字节流到字符流的桥:它读取字节,并使用指定的charset将其解码为字符,它使用的字符集可以由名称指定,也可以被明确指定,或者可以接受平台的默认字符集
牵涉到解码,底层是字节流,但是会解码为字符。如果解码失败就意味着咱们读取失败了
一般不会使用字符流操作图片,音频,视频等,因为牵涉到解码。会出问题!!!开发一般使用字节流!!!
package com.qfedu.task; import java.io.*; public class Demo4 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new FileReader(("F:/aaa/斗破苍穹.txt"))); BufferedWriter bw = new BufferedWriter(new FileWriter(("F:/aaa/ccc/斗破苍穹.txt"))); char[] chs = new char[4 * 1024]; int length; while ((length = br.read(chs)) != -1) { bw.write(chs, 0, length); } bw.close(); br.close(); } }综合案例
package com.qfedu.task; import java.io.*; import java.util.Scanner; public class Demo5 { public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(System.in); System.out.println("请选择使用方法1.字节IO流 2.字符IO流"); int num = scanner.nextInt(); long start = System.currentTimeMillis(); if (num == 1) { txtOrMp3OrMp4Copy(); } else if (num == 2) { txtCopy(); } else { System.out.println("输入错误!!!"); } long end = System.currentTimeMillis(); System.out.println("复制成功,共用时" + (end - start) / 1000 + "秒"); } //字节IO流 public static void txtOrMp3OrMp4Copy() throws IOException { Scanner scanner = new Scanner(System.in); System.out.println("请输入需要复制的文件存放绝对路径"); BufferedInputStream bis = new BufferedInputStream(new FileInputStream((scanner.next()))); System.out.println("请输入复制后的文件存放绝对路径"); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream((scanner.next()))); byte[] bytes = new byte[4 * 1024]; int length; while ((length = bis.read(bytes)) != -1) { bos.write(bytes, 0, length); } bos.close(); bis.close(); } //字符IO流 public static void txtCopy() throws IOException { Scanner scanner = new Scanner(System.in); System.out.println("请输入需要复制的文件存放绝对路径"); BufferedReader br = new BufferedReader(new FileReader((scanner.next()))); System.out.println("请输入复制后的文件存放绝对路径"); BufferedWriter bw = new BufferedWriter(new FileWriter((scanner.next()))); char[] chs = new char[4 * 1024]; int length; while ((length = br.read(chs)) != -1) { bw.write(chs, 0, length); } bw.close(); br.close(); } }