Java的IO流之缓冲流

Java的IO流之缓冲流

  • 缓冲流的概念
  • 字节缓冲流
    • 缓冲输出流 BufferedOutputStream;缓冲输入流BufferedInputStream
  • 字符缓冲流
    • 字符输入缓冲流BufferedReader;字符输出缓冲流BufferedWriter;
  • 综合用法

缓冲流的概念

读取大量数据文件时,读取的速度慢,Java提供了一套缓冲流,提高IO流的效率。它分为字节缓冲流和字符缓冲流。

字节缓冲流

缓冲输出流 BufferedOutputStream;缓冲输入流BufferedInputStream

用法如下:
字节缓冲输出流:

public static void main(String[] args) throws Exception {
	BufferedOutputStream bos = new BufferedOutputStream(new 			FileOutputStream("c:\\buffer"));
    bos.write(65);
    byte[] b= "hello world".getBytes();
    bos.write(b);
    bos.write(b, 3, 2);
    bos.close();
	}

字节缓冲输入流:

public static void main(String[] args) throws Exception {
    	BufferedInputStream b = new BufferedInputStream(new FileInputStream("c:\\buffer"));
	    byte[] bs = new byte[1024];
	    b.close();
	    int len = 0;
	    while((len=b.read(bs))!=-1){
	    	System.out.print(new String(bs,0,len));
	    }
     }

字符缓冲流

字符输入缓冲流BufferedReader;字符输出缓冲流BufferedWriter;

用法如下:

public static void main(String[] args) throws IOException {
		//创建字符输出流  封装文件
		FileWriter fWriter = new FileWriter("c://bu.txt");
		BufferedWriter bufferedWriter = new BufferedWriter(fWriter);
		bufferedWriter.write("您好");
		bufferedWriter.newLine();
		bufferedWriter.write(100);
		bufferedWriter.flush();
		bufferedWriter.write("阿萨德九块九卡");
		bufferedWriter.close();
	}
public static void main(String[] args) throws Exception {
		//创建字符输入缓冲流对象
		BufferedReader bu =new BufferedReader(new FileReader("c:\\bu.txt"));
		String len = null;
		while((len=bu.readLine())!=null){
			System.out.println(len);
		}
		bu.close();
	}

综合用法

、public class Copy {
	public static void main(String[] args) throws Exception {
		long timeStart = System.currentTimeMillis();
		copy4(new File("c:\\liu.mp4"), new File("d:\\liu.mp4"));
		long end = System.currentTimeMillis();
		System.out.println(end-timeStart);
	}
	// 4 字节流缓冲区读写字节数组  
	public static void copy4(File src,File desc) throws Exception{
		BufferedInputStream bu = new BufferedInputStream(new FileInputStream(src)); 
		BufferedOutputStream ou = new BufferedOutputStream(new FileOutputStream(desc)); 
	  int len = 0;
	  byte[] b=new byte[1024*10];
	  while((len=bu.read(b))!=-1){
		  ou.write(b,0,len);
		  
	  }
	  ou.close();
	  bu.close();
	}
	//3 字节流缓冲区 读写单个字节
	public static void copy3(File src,File desc) throws Exception{
		BufferedInputStream bu = new BufferedInputStream(new FileInputStream(src)); 
		BufferedOutputStream ou = new BufferedOutputStream(new FileOutputStream(desc)); 
	  int len = 0;
	  while((len=bu.read())!=-1){
		  ou.write(len);
		  
	  }
	  ou.close();
	  bu.close();
	}
	//2 字节流读写字节数组
	public static void  copy2(File src,File desc) throws Exception{
		FileInputStream fis = new FileInputStream(src);
		FileOutputStream fos = new FileOutputStream(desc);
		int len = 0;
		byte[] b =new byte[1024*10];
		while((len=fis.read(b))!=-1){
			fos.write(b,0,len);
		}
		fos.close();
		fis.close();
	}
	public static void  copy1(File src,File desc) throws Exception{
		FileInputStream fis = new FileInputStream(src);
		FileOutputStream fos = new FileOutputStream(desc);
		int len = 0;
		while((len=fis.read())!=-1){
			fos.write(len);
		}
		fos.close();
		fis.close();
	}
}

你可能感兴趣的:(java)