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[size];
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异常,
}
}
}
}
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[size];
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异常,
}
}
}
}