java基础----字节缓冲流和字符缓冲流

package com.henu.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/*
 * •public BufferedInputStream(InputStream in) :创建一个 新的缓冲输入流。 
	•public BufferedOutputStream(OutputStream out): 创建一个新的缓冲输出流。
	
	使用字节流复制大文件有4中方法
	1.基本流2.基本流加数组
	3.缓冲流(底层有一个数组)
	4.缓冲流加数组(时间最少)
	代码演示直接第四种	
 */

public class Demo13_字节缓冲流 {

	public static void main(String[] args) throws Exception {
		
		bufferedArray();
		
	}
	
	public static void bufferedArray() throws Exception {
		long start = System.currentTimeMillis();
		
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\中公软件包\\mysql-essential-5.1.53-win32.msi"));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\中公软件包\\mysql-essential-5.1.53-win32_copy.msi"));
		byte[] bs = new byte[1024];
		int len = 0;
		while ((len = bis.read(bs)) != -1) {
			bos.write(bs,0,len);
		}
		bos.close();
		bis.close();
		long end = System.currentTimeMillis();
		System.out.println("复制完毕,用时:"+(end-start));
		//复制完毕,用时:105
	}

}
package com.henu.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;

/*
 * •BufferedReader:public String readLine(): 读一行文字。 
•BufferedWriter:public void newLine(): 写一行行分隔符,由系统属性定义符号。 
 */
public class Demo14字符缓冲流 {

	public static void main(String[] args) throws Exception {
		
		//复制文件
		BufferedReader br = new BufferedReader(new FileReader("d:/lmdhk.txt"));
		BufferedWriter bw = new BufferedWriter(new FileWriter("d:/lmdhk_copy.txt"));
		String line = null;
		while ((line = br.readLine()) != null) {
			bw.write(line);
			bw.newLine();
			bw.flush();
		}
		bw.close();
		bw.close();
		System.out.println("复制完毕");
		
		
	}

}

 

 

你可能感兴趣的:(【JAVA】JAVA基础)