字节缓冲流 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Demo06 { public static void main(String[] args) throws IOException { //method1();//写入指定文件 //method2();//读取指定文件 copy1();//复制文件 //copy2();//加Buffered复制文件 } 字节缓冲输出流BufferedOutputStream //字节输出缓冲流 public static void method1() throws IOException{ //明确目的地 FileOutputStream fos=new FileOutputStream("E:\java\demo.txt"); //加入缓冲流 BufferedOutputStream bos=new BufferedOutputStream(fos); bos.write(100); //释放资源 bos.close(); } 字节缓冲输入流 BufferedInputStream //字节输入缓冲流 public static void method2() throws IOException{ //明确目的地 FileInputStream fis=new FileInputStream("E:\java\demo.txt"); //加入缓冲流 BufferedInputStream bis=new BufferedInputStream(fis); int len=0; while((len=bis.read())!=-1){ System.out.println((char)len); } //释放资源 bis.close(); } //单个字节文件复制需要多长时间 public static void copy1() throws IOException{ FileInputStream fis=new FileInputStream("E:\java\apache-tomcat-7.0.52(x86).zip"); FileOutputStream fos=new FileOutputStream("E:\java\a\apache-tomcat-7.0.52(x86).zip"); long time1=System.currentTimeMillis(); int len=0; while((len=fis.read())!=-1){ fos.write(len); } long time2=System.currentTimeMillis(); System.out.println(time2-time1); fis.close(); fos.close(); } //单个字节文件复制需要多长时间 加Buffered public static void copy2() throws IOException{ FileInputStream fis=new FileInputStream("E:\java\apache-tomcat-7.0.52(x86).zip"); BufferedInputStream bis=new BufferedInputStream(fis); FileOutputStream fos=new FileOutputStream("E:\java\a\apache-tomcat-7.0.52(x86).zip"); BufferedOutputStream bos=new BufferedOutputStream(fos); long time1=System.currentTimeMillis(); int len=0; while((len=bis.read())!=-1){ bos.write(len); } long time2=System.currentTimeMillis(); System.out.println(time2-time1); bis.close(); bos.close(); } } 字符缓冲流 import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Demo07 { public static void main(String[] args) throws IOException { method1();//字符文件写入 method2();//字符文件读出 method3();//字符文件读出 copy();//文件复制 } 字符缓冲输出流 BufferedWriter public static void method1() throws IOException{ //明确目的地 FileWriter fw=new FileWriter("E:\java\demo01.txt"); //加缓冲流 BufferedWriter bw=new BufferedWriter(fw); //写入文件 bw.write("打盹啊"); bw.newLine();//换行 bw.flush(); bw.write("真的"); bw.flush(); bw.write("不骗你"); bw.close(); } 字符缓冲输入流 BufferedReader public static void method2() throws IOException{ //明确数据源 FileReader fr=new FileReader("E:\java\demo01.txt"); //添加缓冲流 BufferedReader br=new BufferedReader(fr); //开始读取文件 String line=br.readLine(); System.out.println(line); line=br.readLine(); System.out.println(line); line=br.readLine(); System.out.println(line); line=br.readLine(); System.out.println(line); br.close(); } public static void method3() throws IOException{ //明确数据源 FileReader fr=new FileReader("E:\java\demo01.txt"); //添加缓冲流 BufferedReader br=new BufferedReader(fr); //开始读取文件 String line=""; while((line=br.readLine())!=null){ System.out.print(line);//只读内容不读换行 } br.close(); } 复制文件 public static void copy() throws IOException{ //明确数据源 FileReader fr=new FileReader("E:\java\demo01.txt"); BufferedReader br=new BufferedReader(fr); //明确目的地 FileWriter fw=new FileWriter("E:\java\d.txt"); BufferedWriter bw=new BufferedWriter(fw); //开始复制 String line=""; while((line=br.readLine())!=null){ bw.write(line); bw.newLine(); bw.flush(); } br.close(); bw.close(); }