java字节流复制一个avi文件

public class CopyAvi {
	public static void main(String[] args) throws IOException {
		
		copy_3();
	}
	
	private static void copy_3() throws IOException {
		FileInputStream fis = new FileInputStream("C:\\Users\\Administrator\\Desktop\\1.avi");
		FileOutputStream fos = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\4.avi");
		
		//创建一个大小正好的数组,一般不建议用,文件太大消耗内存大
		byte[] buf = new byte[fis.available()];
		
		int len = 0;
		
		fis.read(buf);
		fos.write(buf);
		
		fis.close();
		fos.close();
		
	}

	//自带的缓冲区
	private static void copy_2() throws IOException {
		FileInputStream fis = new FileInputStream("C:\\Users\\Administrator\\Desktop\\1.avi");
		BufferedInputStream bis = new BufferedInputStream(fis);
		FileOutputStream fos = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\3.avi");
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		
		
		int ch = 0;
		
		while((ch=bis.read())!=-1){
			bos.write(ch);
		}
		
		bis.close();
		bos.close();
	}

	private static void copy_1() throws IOException {

		FileInputStream fis = new FileInputStream("C:\\Users\\Administrator\\Desktop\\1.avi");
		FileOutputStream fos = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\2.avi");
		
		byte[] buf = new byte[1024];
		
		int len = 0;
		
		while((len=fis.read(buf))!=-1){
			fos.write(buf,0,len);
		}
		
		fis.close();
		fos.close();
	}
}

你可能感兴趣的:(java)