copy的效率

import java.io.*;

class  CopyMp3
{
	public static void main(String[] args) throws IOException
	{

		long start = System.currentTimeMillis();

			copy05();
		long end = System.currentTimeMillis();

		System.out.println((end-start)+"毫秒");
	}
	/*
	通过自定义缓冲区 。30
	*/
	public static void copy01()throws IOException
	{
		FileInputStream fis = new FileInputStream("f:\\0.mp3");
		FileOutputStream fos =  new FileOutputStream("f:\\1.mp3");

		byte[] arr = new byte[1024*8];

		int num = 0;

		while((num=fis.read(arr))!=-1)
		{
			fos.write(arr,0,num);
		}

		fos.close();
		fis.close();
	}
	/*
	建立一个刚刚好的缓冲区。140.
	*/
	public static void copy02()throws IOException
	{
		FileInputStream fis = new FileInputStream("f:\\0.mp3");
		FileOutputStream fos =  new FileOutputStream("f:\\2.mp3");

		byte[] arr = new byte[fis.available()];

		fis.read(arr);

		fos.write(arr);

		fos.close();
		fis.close();
	}
	/*
	使用Buffer 562
	*/
	public static void copy03()throws IOException
	{
		FileInputStream fis = new FileInputStream("f:\\0.mp3");
		BufferedInputStream bufis = new BufferedInputStream(fis);


		FileOutputStream fos =  new FileOutputStream("f:\\3.mp3");
		BufferedOutputStream bufos = new BufferedOutputStream(fos);

		int num = 0;

		while((num=bufis.read())!=-1)
		{
			bufos.write(num);
		}

		bufos.close();
		bufis.close();
	}
	/*
	不适用缓冲区
	*/
	public static void copy04()throws IOException
	{
		FileInputStream fis = new FileInputStream("f:\\0.mp3");
		FileOutputStream fos =  new FileOutputStream("f:\\4.mp3");


		int num = 0;

		while((num=fis.read())!=-1)
		{
			fos.write(num);
		}

		fos.close();
		fis.close();
	}

	/*
	模拟一个BufferInputStream
	*/
	public static void copy05()throws IOException
	{
		FileInputStream fis = new FileInputStream("f:\\9.mp3");
		MyBufferedInputStream bufis = new MyBufferedInputStream(fis);


		FileOutputStream fos =  new FileOutputStream("f:\\8.mp3");
		BufferedOutputStream bufos = new BufferedOutputStream(fos);

		int num = 0;

		while((num=bufis.myRead())!=-1)
		{
			bufos.write(num);
		}

		bufos.close();
		bufis.myClose();
	}
}



//11111111    1111111111111110000100101001010

 

你可能感兴趣的:(copy)