JAVA-IO字节流两种拷贝图片的方法
前言:
JAVA-IO字节流两种拷贝图片的方法,如果对你有帮助就看看吧。
正文:
复制一个图片思路:
- 1.用字节读取流对象和图片关联。
- 2.用字节写入流对象创建一个图片文件,用于存储获取到的图片数据。
- 3.通过循环读写,完全数据的存储
- 4.关闭资源
import java.io.*;
public class IO流拷贝图片_11 {
public static void main(String[] args){
try {
copy();
}
catch (IOException e){
throw new RuntimeException("错误");
}
FileOutputStream fos = null;
FileInputStream fis = null;
try{
fis = new FileInputStream("D:\\桌面文件\\1.jpg");
fos = new FileOutputStream("D:\\桌面文件\\2.jpg");
byte[] buf = new byte[1024];
int len = 0;
while((len = fis.read(buf)) != -1){
fos.write(buf , 0 , len);
}
}
catch (IOException e){
throw new RuntimeException("错误");
}
finally {
if (fis != null) {
try{
fis.close();
}
catch (IOException e){
throw new RuntimeException("错误");
}
}
if (fos != null) {
try{
fos.close();
}
catch (IOException e){
throw new RuntimeException("错误");
}
}
}
}
public static void copy() throws IOException{
FileInputStream you = new FileInputStream("D:\\桌面文件\\1.jpg");
FileOutputStream meiyou = new FileOutputStream("D:\\桌面文件\\3.jpg");
BufferedInputStream bufis = new BufferedInputStream(you);
BufferedOutputStream bufos = new BufferedOutputStream(meiyou);
int len = 0;
while ((len = bufis.read()) != -1){
bufos.write(len);
}
bufis.close();
bufos.close();
}
}
资源均来自第三方,谨慎下载,前往第三方网站下载


