使用java的IO字节流拷贝图片

需求: 拷贝一张图片。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyImage {
	
	
	public static void main(String[] args) throws IOException {
		//找到目标文件
		File inFile = new File("F:\\美女\\1.jpg");
		File destFile = new File("E:\\1.jpg");
		//建立数据的输入输出通道
		FileInputStream fileInputStream = new  FileInputStream(inFile);
		FileOutputStream fileOutputStream = new FileOutputStream(destFile); //追加数据....
		
		//每新创建一个FileOutputStream的时候,默认情况下FileOutputStream 的指针是指向了文件的开始的位置。 每写出一次,指向都会出现相应移动。
		//建立缓冲数据,边读边写
		byte[] buf = new byte[1024]; 
		int length = 0 ; 
		while((length = fileInputStream.read(buf))!=-1){ //最后一次只剩下了824个字节
			fileOutputStream.write(buf,0,length); //写出很多次数据,所以就必须要追加。
		}
		//关闭资源 原则: 先开后关,后开先关。
		fileOutputStream.close();
		fileInputStream.close();
	}

}


带异常处理的方式

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

import javax.management.RuntimeErrorException;

/*
 IO异常 的处理

 */
public class Demo1 {

	public static void main(String[] args) {
	//	readTest();
		
		copyImage();
	}

	// 拷贝图片
	public static void copyImage() {
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;

		try {
			// 找到目标文件
			File inFile = new File("F:\\美女\\1.jpg");
			File outFile = new File("E:\\1.jpg");
			// 建立输入输出通道
			fileInputStream = new FileInputStream(inFile);
			fileOutputStream = new FileOutputStream(outFile);
			// 建立缓冲数组,边读边写
			byte[] buf = new byte[1024];
			int length = 0;
			while ((length = fileInputStream.read(buf)) != -1) {
				fileOutputStream.write(buf, 0, length);
			}
		} catch (IOException e) {
			System.out.println("拷贝图片出错...");
			throw new RuntimeException(e);
		} finally {
			// 关闭资源
			try {
				if (fileOutputStream != null) {
					fileOutputStream.close();
					System.out.println("关闭输出流对象成功...");
				}
			} catch (IOException e) {
				System.out.println("关闭输出流资源失败...");
				throw new RuntimeException(e);
			} finally {
				if (fileInputStream != null) {
					try {
						fileInputStream.close();
						System.out.println("关闭输入流对象成功...");
					} catch (IOException e) {
						System.out.println("关闭输入流对象失败...");
						throw new RuntimeException(e);
					}
				}

			}
		}
	}

	public static void readTest() {
		FileInputStream fileInputStream = null;
		try {
			// 找到目标文件
			File file = new File("F:\\aaaaa.txt");
			// 建立数据输入通道
			fileInputStream = new FileInputStream(file);
			// 建立缓冲数组读取数据
			byte[] buf = new byte[1024];
			int length = 0;
			while ((length = fileInputStream.read(buf)) != -1) {
				System.out.print(new String(buf, 0, length));
			}
		} catch (IOException e) {
			/*
			 * //处理的代码... 首先你要阻止后面的代码执行,而且要需要通知调用者这里出错了... throw new
			 * RuntimeException(e);
			 * //把IOException传递给RuntimeException包装一层,然后再抛出,这样子做的目的是
			 * 为了让调用者使用变得更加灵活。
			 */
			System.out.println("读取文件资源出错....");
			throw new RuntimeException(e);
		} finally {
			try {
				if (fileInputStream != null) {
					fileInputStream.close();
					System.out.println("关闭资源成功...");
				}
			} catch (IOException e) {
				System.out.println("关闭资源失败...");
				throw new RuntimeException(e);
			}
		}
	}

}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值