1. 程式人生 > >java緩衝位元組流複製檔案,逐個位元組讀取、寫入

java緩衝位元組流複製檔案,逐個位元組讀取、寫入

package cwj.bbb;

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;

class StreamTest
{
	public static void main(String[] args) throws IOException
	{
		/*把路徑下的檔案/home/cwjy1202/hadoop/javaTest/input01.txt
		 * 以位元組緩衝輸出流的方式複製到/home/cwjy1202/hadoop/javaTest/input013.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/input013.txt"));
		
		//逐個位元組讀取
		int len = bis.read();
		while(-1 != len)
		{
			//逐個位元組寫入
			bos.write(len);
			len = bis.read();
		}
		
		//強制把緩衝區的資料輸出
		bos.flush();
		bos.close();
		bis.close();
	}
}