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();
}
}
}
}
}
java使用IO流拷贝图片文件
最新推荐文章于 2022-07-24 21:16:28 发布