package cn.zhozuohou; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; /* * 一、缓冲流? * 【缓冲流的目的就是提高读写效率。】 * * 二、字节输入输出缓冲流! 1.字节输入缓冲流! BufferedInputStream 读! read() 复制!文件到程序! BufferedInputStream a1=new BufferedInputStream(new FileInputStream(文件)); 2.字节输出缓冲流! BufferedOutputStream 写! write() 拷贝!程序到文件! FileOutputStream a1=new FileOutputStream("文件); 3.字节缓冲流拷贝文件,效率非常高! 1.先读后写 BufferedInputStream a1=new BufferedInputStream (new FileInputStream("D:\\XMind\\1.dat")); BufferedOutputStream a2=new BufferedOutputStream(new FileOutputStream("d:\\game\\1.dat")); 三、字符输入输出缓冲流! 1.字符输入缓冲流。读!read BufferedReader; BufferedReader a1=new BufferedReader(new FileReader(文件名)); 字符类型 String char 2.字符输出缓冲流! 写! write BufferedWrite; FileWriter a1=new FileWriter(文件名); 3.字符缓冲流拷贝文件! BufferedReader a1=new BufferedReader(new FileReader("d:\\game\\abc.txt")); BufferedWriter a2=new BufferedWriter(new FileWriter("d:\\day13\\abc.txt")); * * */ public class 缓冲流 { public static void main(String[] args) throws IOException { bufferedInputStream(); //1.字节输入缓冲流 读! 慢 快! bufferedOutputStream(); //2.字节输出缓冲流 // copy01(); //3.字节缓冲流拷贝文件! 速度快! // copy02(); //4.普通字节流拷贝文件! bufferedReader(); //5.字符输入缓冲流! bufferedWriter(); //6.字符输出缓冲流! copy03(); //7.字符缓冲流流复制文件! } //7.字符缓冲流流复制文件! private static void copy03() throws IOException{ BufferedReader a1=new BufferedReader(new FileReader("d:\\game\\aa.txt")); BufferedWriter a2=new BufferedWriter(new FileWriter("C:\\aa.txt")); int len=0; char[]b=new char[1024*10000]; while ((len=a1.read(b))!=-1) { a2.write(b,0,len); } a1.close(); a2.close(); } //6.字符输出缓冲流! private static void bufferedWriter() throws IOException{ BufferedWriter a1=new BufferedWriter(new FileWriter("d:\\game\\aaa.txt")); a1.write("你好"); a1.flush(); String b="zhou xu feng"; a1.write(b,0,8); a1.flush(); a1.close(); } //5.字符输入缓冲流! private static void bufferedReader() throws IOException { int num=0; BufferedReader a1=new BufferedReader(new FileReader("d:\\game\\aa.txt")); String line; //缓冲流特有方法 读取文本单行! byte []b=new byte[1024*10]; //快速读取! while ((line=a1.readLine())!=null) { //集合非空! num++; System.out.println("第"+num+"行的内容 "+line); } a1.close(); } //4.普通字节流拷贝文件! private static void copy02() throws IOException{ long t1=System.currentTimeMillis(); FileInputStream a1=new FileInputStream("E:\\老师的代码\\day27\\视屏\\1.mp4"); FileOutputStream a2=new FileOutputStream("d:\\1.mp4"); int len=0; byte []b=new byte[1024*10000]; while ((len=a1.read(b))!=-1) { a2.write(b,0,len); } a1.close(); a2.close(); long t2=System.currentTimeMillis(); System.out.println("利用字节流复制文件的时间!"+(t2-t1)+"毫秒"); } //3.字节缓冲流拷贝文件! 速度快! private static void copy01() throws IOException{ long t1=System.currentTimeMillis(); BufferedInputStream a1=new BufferedInputStream(new FileInputStream("E:\\老师的代码\\day27\\视屏\\1.mp4")); BufferedOutputStream a2=new BufferedOutputStream(new FileOutputStream("d:\\1.mp4")); int len=0; byte []b=new byte[1024*10000]; while ((len=a1.read(b))!=-1) { a2.write(b,0,len); } a1.close(); a2.close(); long t2=System.currentTimeMillis(); System.out.println("利用字节缓冲流复制文件的时间!"+(t2-t1)+"毫秒"); } //2.字节输出缓冲流 private static void bufferedOutputStream() throws IOException { BufferedOutputStream a1=new BufferedOutputStream(new FileOutputStream("d:\\game\\aa.txt")); a1.write(100); //添加字符串! byte b[]="zhou xu feng".getBytes(); a1.write(b,0,12); a1.close(); } //1. 字节输入缓冲流 读! 慢 快! private static void bufferedInputStream() throws IOException{ BufferedInputStream a1=new BufferedInputStream(new FileInputStream("d:\\game\\aa.txt")); /*1.快速读取方式 int len=0; byte[] b=new byte[1024*10]; while ((len=a1.read(b))!=-1) { System.out.println(new String(b,0,len)); }*/ //普通方式! int len=0; while ((len=a1.read())!=-1) { System.out.print((char)len+" "); //直接输出len 是一堆数字 通过(char)将他们转换成字符 } a1.close(); System.out.println("==================="); } }