IO流拷贝图片的四种方式

package IO;

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

public class CopyImg {

	/**
	 *
	 * IO流复制图片(记事本打开不能读懂),使用字节流
	 * 4种方式,推荐使用第四种
	 * 
	 */
	public static void main(String[] args) throws IOException {
		//数据源
		File srcfile=new File("数据源");
		//目的地
		File destfile=new File("目的地");
		//methd1(srcfile,destfile);
		//methd2(srcfile,destfile);
		//methd3(srcfile,destfile);
		methd4(srcfile,destfile);
	}
	
	//方式四:缓冲字节流一次读写一个字节数组
	private static void methd4(File srcfile, File destfile) throws IOException {
		BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcfile));
		BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destfile));
		byte[] by=new byte[1024];
		int length=0;
		while((length=bis.read(by))!=-1){
			bos.write(by,0,length);
		}
		//关闭资源
		bos.close();
		bis.close();
	}
	/*
	//方式三:缓冲字节流一次读写一个字节
	private static void methd3(File srcfile, File destfile) throws IOException {
		BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcfile));
		BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destfile));
		int by=0;
		while((by=bis.read())!=-1){
			bos.write(by);
		}
		bos.close();
		bis.close();
	}
	*/
	/*
	//方式二:基本字节流一次读写一个字节数组
	private static void methd2(File srcfile, File destfile) throws IOException {
		FileInputStream fis=new FileInputStream(srcfile);
		FileOutputStream fos=new FileOutputStream(destfile);
		byte[] by=new byte[1024];
		int length=0;
		while((length=fis.read(by))!=-1){
			fos.write(by,0,length);
		}
		fos.close();
		fis.close();
	}
	*/
	/*
	//方式一:基本字节流一次读写一个字节
	private static void methd1(File srcfile, File destfile) throws IOException {
		FileInputStream fis=new FileInputStream(srcfile);
		FileOutputStream fos=new FileOutputStream(destfile);
		int by=0;
		while((by=fis.read())!=-1){
			fos.write(by);
		}
		fos.close();
		fis.close();
	}
	*/
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值