---
title: "图片写入"
author: "qdexception"
date: "2010-04-29T21:59:34Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2010-04-29T21:59:34Z"
id: 656559
url: "https://qdexception.iteye.com/blog/656559"
---

Code:

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.IOException;
public class ImgWriteUtil {
 private BufferedInputStream bis;
 private BufferedOutputStream bos;
 public void writeImg(File fromfile, File tofile) {//写入图片
  int len = 0;
  int size = 0;
  try {
   bis = new BufferedInputStream(new FileInputStream(fromfile));
   bos = new BufferedOutputStream(new FileOutputStream(tofile));
   size = (int) fromfile.length();
   byte buffer = new byte;
   while ((len = bis.read(buffer)) >0) {
    bos.write(buffer, 0, len);
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
   throw new RuntimeException();//抛出unchecked异常，
  } catch (IOException e) {
   e.printStackTrace();
   throw new RuntimeException();//抛出unchecked异常，;
  } finally {
   try {
    if (bos != null) {
     bos.close();
    }
    if (bis != null) {
     bis.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
    throw new RuntimeException();//抛出unchecked异常，
   }
  }
 }
}