java缓冲字节流的复制数据,数据以数组读取、写入,效率更高

package cwj.bbb;

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


class StreamTest
{
	public static void main(String[] args) throws IOException
	{
		/*将路径/home/cwjy1202/hadoop/javaTest/input01.txt下的文件
		 * 以缓冲字节流的方式复制到/home/cwjy1202/hadoop/javaTest/input014.txt
		 * 数据以数组读取、写入,效率更高
		 * */
		File file = new File("/home/cwjy1202/hadoop/javaTest/input01.txt");		
		InputStream bis = new BufferedInputStream(new FileInputStream(file));
		OutputStream bos = new BufferedOutputStream(new FileOutputStream("/home/cwjy1202/hadoop/javaTest/input014.txt"));
		
		byte[] b = new byte[1024];
		//数组读取
		int len = bis.read(b);
		while(-1 != len)
		{
			//数组写入
			bos.write(b, 0, len);
			len = bis.read(b);
		}
		
		//刷新缓冲区,将缓冲区的数据强制写到目标文件中
		bos.flush();
		bos.close();
		bis.close();	
	}
}


你可能感兴趣的:(Java)