复制图片的四种方式

来源:互联网 发布:竞争学习算法 编辑:程序博客网 时间:2024/04/28 13:50

复制图片的四种方式

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;

 

/*

 * 复制图片

 * 分析:

 *     复制数据,如果我们知道用记事本打开并能够读懂,就用字符流,否则用字节流

 *     通过该原理,我们知道我们应该采用字节流更方便些

 *     而字节流有4种方式,所以做这个题目我们有4中方式,推荐掌握第4中方式

 * 数据源:

 *    e:\\照片.jpg -- FileInputStream -- BufferedInputStream

 * 目的地:

 *    f:\\照片1.jpg -- FileOutputStream -- BufferedOutputStream

 */

public class CopyImage {

 

         publicstatic void main(String[] args) throws IOException {

                   //TODO Auto-generated method stub

 

                   //可以使用字符串作为路径也可以使用File对象作为参数

                   FilesrcFile = new File("照片.jpg");  //c:\\照片.jpg

                   FiledesFile = new File("照片1.jpg");

                  

//               method1(srcFile,desFile);

//               method2(srcFile,desFile);

//               method3(srcFile,desFile);

                   method4(srcFile,desFile);

         }

 

         //字节缓冲流一次读写一个数组

         publicstatic void method4(File srcFile, File desFile) throws IOException {

                   //TODO Auto-generated method stub

                  

                   BufferedInputStreambis = new BufferedInputStream(new FileInputStream(srcFile));

                   BufferedOutputStreambos = new BufferedOutputStream(new FileOutputStream(desFile));

                  

                   byte[]bys = new byte[1024];

                   intlen = 0;

                   while((len= bis.read(bys)) != -1){

                            bos.write(bys,0,len);

                   }

                   bos.close();

                   bis.close();

         }

 

         //字节缓冲流一次读写一个字节

         publicstatic void method3(File srcFile, File desFile) throws IOException {

                   //TODO Auto-generated method stub

                  

                   BufferedInputStreambis = new BufferedInputStream(new FileInputStream(srcFile));

                   BufferedOutputStreambos = new BufferedOutputStream(new FileOutputStream(desFile));

                  

                   intby = 0;

                   while((by= bis.read()) != -1){

                            bos.write(by);

                   }

                   bos.close();

                   bis.close();

         }

 

         //基本字节流一次读写一个字节数组

         publicstatic void method2(File srcFile, File desFile) throws IOException {

                   //TODO Auto-generated method stub

                  

                   FileInputStreamfis = new FileInputStream(srcFile);

                   FileOutputStreamfos = new FileOutputStream(desFile);

                  

                   byte[]bys = new byte[1024];

                   intlen = 0;

                   while((len= fis.read(bys)) != -1){

                            fos.write(bys,0,len);

                   }

                   fos.close();

                   fis.close();

         }

 

         //基本字节流一次读写一个字节

         publicstatic void method1(File srcFile, File desFile) throws IOException {

                   //TODO Auto-generated method stub

                  

                   FileInputStreamfis = new FileInputStream(srcFile);

                   FileOutputStreamfos = new FileOutputStream(desFile);

                  

                   intby = 0;

                   while((by= fis.read()) != -1){

                            fos.write(by);

                   }

                   fos.close();

                   fis.close();

         }

 

}

0 0