java使用IO流拷贝图片文件

本文介绍如何使用Java的IO流进行图片文件的复制操作,详细讲解了相关代码实现,包括创建输入流、输出流,以及流的关闭等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.heilong.io;

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.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class copy_image {

	public static void main(String[] args){
		copyImage1();
	}

	//方式三:使用FileReader和FileWriter拷贝图片
	public static void copyImage3(){
		
		//2.建立数据的输入输出通道
		FileReader fileReader = null;
		FileWriter fileWriter = null;
		try {
			//1.找到目标文件
			File inFile = new File("./src/data/1.0.PNG");
			File destFile = new File("./src/data/1.0.copy3.PNG");
			
			fileReader = new FileReader(inFile);
			fileWriter = new FileWriter(destFile);
			
			//3.拷贝图片
			char[] buf = new char[1024];
			int length = 0;
			while((length=fileReader.read(buf)) != -1){
				fileWriter.write(buf, 0, length);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				//4.关闭资源
				fileWriter.close();
			} catch (IOException e) {
				e.printStackTrace();
			}finally {
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		
	}
	
	//方式二:使用BufferedInputStream和BufferedOutputStream拷贝图片
	public static void copyImage2(){
		
		//3.建立缓冲输入输出字节流
		BufferedInputStream bufferedInputStream = null;
		BufferedOutputStream bufferedOutputStream = null;
		try {
			//1.找到目标文件
			File infile = new File("./src/data/1.0.PNG");
			File destFile = new File("./src/data/1.0.copy2.PNG");
			
			//2.建立数据的输入输出通道
			FileInputStream fileInputStream = new FileInputStream(infile);
			FileOutputStream fileOutputStream = new FileOutputStream(destFile);
			
			bufferedInputStream = new BufferedInputStream(fileInputStream);
			bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
			
			//4.拷贝图片
			int content = 0;
			while((content=bufferedInputStream.read()) != -1){
				bufferedOutputStream.write(content);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			//5.关闭资源
			try {
				bufferedOutputStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				try {
					bufferedInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		
	}
	
	
	//方式一:使用FileInputStream和FileOutputStream拷贝图片
	public static void copyImage1() {
		
		//2.建立数据的输入输出通道
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		
		try {
			//1.找到源文件和目的文件
			File inFile = new File("./src/data/1.0.PNG");
			File destFile = new File("./src/data/1.0._copy1.PNG");
			
			fileInputStream = new FileInputStream(inFile);
			fileOutputStream = new FileOutputStream(destFile);
			
			//3.拷贝图片
			byte[] buf = new byte[1024];
			int length = 0;
			while((length=fileInputStream.read(buf)) != -1){
				fileOutputStream.write(buf, 0, length);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			//4.关闭资源
			try {
				fileOutputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}finally {
				try {
					fileInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		
		
		
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值