- 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 ImageCopier {
- public static void main(String args[]) throws Exception{
- //源图片路径(包括图片名)
- String srcUrl = "D:/My Documents/temp/p1.jpg";
- //目标图片路径
- String tarUrl = "D:/My Documents/temp/p2.jpg";
- ioImage(srcUrl, tarUrl);
- }
- /**
- *
- * @param srcUrl 源图片路径
- * @return 目标图片路径
- */
- public static void ioImage(String srcUrl,String tarUrl){
- try {
- //生成读取图片到内存的输入流
- FileInputStream finput = new FileInputStream(new File(srcUrl));
- //生成从内存将图片输出到磁盘的输出流
- FileOutputStream foutput = new FileOutputStream(new File(tarUrl));
- BufferedOutputStream bos = new BufferedOutputStream(foutput);
- int b ;
- while(true) {
- if(finput.available()<1024){
- //
- while((b=finput.read())!=-1){
- bos.write(b);
- }
- break;
- }else{
- b=finput.read();
- bos.write(b);
- }
- } finput.close();
- bos.close();
- foutput.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }