IO字節緩衝流BufferedInputStream / BufferedOutputStream

緩衝流是對字節流的緩衝,可以加快字節流的效率。使用緩衝流的前提是必須使用字節流,我們只需要關閉最外面的流就行了,內部的留會自動關閉。

1:緩衝字節輸入流

package com.jianshun;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * 四個步驟: 分段讀取 文件字節輸入流  加入緩衝流
 * 1、創建源
 * 2、選擇流
 * 3、操作
 * 4、釋放資源
 */
public class BufferedTest01 {

	public static void main(String[] args) {
		//1,創建流
		File src = new File("abc.txt");
		//2,選擇流
		InputStream in = null;
		try {
			in = new BufferedInputStream(new FileInputStream(src));
			//3,操作(分段讀取)
			byte[] flush = new byte[1024];//緩衝容器
			int len = -1;
			while((len = in.read(flush))!=-1){
				//字節數組--》字符串 (解碼)
				String str = new String(flush,0,len);
				System.out.println(str);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			//4,釋放資源
			if(null!=in){
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}

}

 2:緩衝字節輸出流

package com.jianshun;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 四個步驟: 分段讀取 文件字節輸入流  加入緩衝流
 * 1、創建源
 * 2、選擇流
 * 3、操作
 * 4、釋放資源
 */
public class BufferedTest02 {

	public static void main(String[] args) {
		File src = new File("dest.txt");
		OutputStream out = null;
		try {
			out = new BufferedOutputStream(new FileOutputStream(src));
			String msg = "IO is so easy\r\n";
			byte[] datas = msg.getBytes();
			out.write(datas, 0, datas.length);
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(null!=out){
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}

}

 

我們可以改進之前複製文件的方法,用字節緩衝流複製文件

package com.jianshun;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
 * 緩衝字節流拷貝文件
 * @author Administrator
 */
public class Copy02 {

	public static void main(String[] args) {
		copy("abc.txt","dest.txt");
	}
	
	public static void copy(String srcPath,String destPath){
		//,2,創建源
		File src = new File(srcPath);
		File dest = new File(destPath);
		//,2,選擇流
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new BufferedInputStream(new FileInputStream(src));
			out =new BufferedOutputStream(new FileOutputStream(dest));
			//3,操作,分段讀取
			byte[] flush = new byte[1024];
			int len = -1;//接收長度
			while((len = in.read(flush)) != -1){
				out.write(flush, 0, len);//分段寫出
			}
			out.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			//4,釋放資源,分別關閉,先打開的後關閉
			if(out != null){
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			if(in !=null){
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void copy2(String srcPath,String destPath){
		File src = new File(srcPath);//源頭
		File dest = new File(destPath);
		try(InputStream in = new BufferedInputStream(new FileInputStream(srcPath));
			OutputStream out = new BufferedOutputStream(new FileOutputStream(destPath))) {
			//操作,分段讀取
			byte[] flush = new byte[1024];
			int len = -1;
			while((in.read(flush))!=-1){
				out.write(flush, 0, len);
			}
			out.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章