A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

代码中copyPic_2和copyPic_3中的while的读取数据部分有什么区别吗?copyPic_2那个为什么会报java.lang.ArrayIndexOutOfBoundsException
代码如下,求大神解答
  1. package FileStreamDemo;

  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;

  8. public class FileStreamDemo {

  9.         public static void main(String[] args) {
  10.                 // TODO Auto-generated method stub
  11.                 long startTime = System.currentTimeMillis();
  12.                 copyPic_2();
  13.                 long endTime = System.currentTimeMillis();
  14.                 System.out.println(endTime-startTime+"毫秒");
  15.         }
  16.        
  17.         public static void copyPic_3(){
  18.                 FileInputStream fis = null;
  19.                 FileOutputStream fos = null;
  20.                
  21.                 try {
  22.                         fis = new FileInputStream("1.jpg");
  23.                         fos = new FileOutputStream("1_copy2.jpg");
  24.                        
  25.                         byte by[] = new byte[1024];
  26.                        
  27.                         int len = 0;
  28.                         try {
  29.                                 while((len=fis.read(by))!=-1){
  30.                                         fos.write(by, 0, len);
  31.                                 }
  32.                         } catch (IOException e) {
  33.                                 // TODO Auto-generated catch block
  34.                                 e.printStackTrace();
  35.                         }
  36.                 } catch (FileNotFoundException e) {
  37.                         // TODO Auto-generated catch block
  38.                         e.printStackTrace();
  39.                 }
  40.                 finally{
  41.                         try {
  42.                                 if(fis!=null)
  43.                                         fis.close();
  44.                         } catch (IOException e) {
  45.                                 // TODO Auto-generated catch block
  46.                                 e.printStackTrace();
  47.                         }
  48.                         try {
  49.                                 if(fos!=null)
  50.                                         fos.close();
  51.                         } catch (IOException e) {
  52.                                 // TODO Auto-generated catch block
  53.                                 e.printStackTrace();
  54.                         }
  55.                 }
  56.         }
  57.         public static void copyPic_2(){
  58.                 BufferedInputStream bufIn= null;
  59.                 BufferedOutputStream bufOut = null;
  60.                
  61.                 try {
  62.                         bufIn = new BufferedInputStream(new FileInputStream("1.jpg"));
  63.                         bufOut = new BufferedOutputStream(new FileOutputStream("1_copy2.jpg"));
  64.                        
  65.                         byte by[] = new byte[1024*1024*200];
  66.                        
  67.                         int len =0;
  68.                         while(len!=-1){
  69.                                 try {
  70.                                         len = bufIn.read(by);
  71.                                         bufOut.write(by, 0, len);
  72.                                 } catch (IOException e) {
  73.                                         // TODO Auto-generated catch block
  74.                                         e.printStackTrace();
  75.                                 }
  76.                         }
  77.                 } catch (FileNotFoundException e) {
  78.                         // TODO Auto-generated catch block
  79.                         e.printStackTrace();
  80.                 }
  81.                 finally{
  82.                         try {
  83.                                 if(bufIn!=null)
  84.                                         bufIn.close();
  85.                         } catch (IOException e) {
  86.                                 // TODO Auto-generated catch block
  87.                                 e.printStackTrace();
  88.                         }
  89.                         try {
  90.                                 if(bufOut!=null)
  91.                                         bufOut.close();
  92.                         } catch (IOException e) {
  93.                                 // TODO Auto-generated catch block
  94.                                 e.printStackTrace();
  95.                         }
  96.                 }
  97.         }
  98.         public static void copyPic_1(){
  99.                 BufferedInputStream bufIn= null;
  100.                 BufferedOutputStream bufOut = null;
  101.                 try {
  102.                         bufIn = new BufferedInputStream(new FileInputStream("1.jpg"));
  103.                         bufOut = new BufferedOutputStream(new FileOutputStream("1_copy.jpg"));
  104.                        
  105.                         int len = 0;
  106.                         while(len!=-1){
  107.                                 try {
  108.                                         len = bufIn.read();
  109.                                         bufOut.write((char)len);
  110.                                 } catch (IOException e) {
  111.                                         // TODO Auto-generated catch block
  112.                                         e.printStackTrace();
  113.                                 }
  114.                         }
  115.                 } catch (FileNotFoundException e) {
  116.                         // TODO Auto-generated catch block
  117.                         e.printStackTrace();
  118.                 }
  119.                 finally{
  120.                         try {
  121.                                 if(bufIn!=null)
  122.                                         bufIn.close();
  123.                         } catch (IOException e) {
  124.                                 // TODO Auto-generated catch block
  125.                                 e.printStackTrace();
  126.                         }
  127.                         try {
  128.                                 if(bufOut!=null)
  129.                                         bufOut.close();
  130.                         } catch (IOException e) {
  131.                                 // TODO Auto-generated catch block
  132.                                 e.printStackTrace();
  133.                         }
  134.                 }
  135.         }

  136. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
lwj123 + 1

查看全部评分

4 个回复

倒序浏览
本帖最后由 l763631191 于 2015-5-11 16:26 编辑

我还是看看下面的回答吧不过byte by[] = new byte[1024*1024*200];  1024*1024*200  200M啊,这难道不内存溢出,你试试1024*2 试试啊
回复 使用道具 举报
l763631191 发表于 2015-5-11 16:17
我还是看看下面的回答吧不过byte by[] = new byte[1024*1024*200];  1024*1024*200  200M啊,这难道不内存 ...

我试了,开始的时候写的是1024,后面又改了1024*2,1024*20,都不行
回复 使用道具 举报
王小乖 发表于 2015-5-11 16:28
我试了,开始的时候写的是1024,后面又改了1024*2,1024*20,都不行

在使用缓冲字节流,你直接用read方法读就可以了 然后再直接写~~,不需要先读到一个数组中,这本身就是加强的流,里面的read()方法就是加强的功能
回复 使用道具 举报
l763631191 发表于 2015-5-11 16:38
在使用缓冲字节流,你直接用read方法读就可以了 然后再直接写~~,不需要先读到一个数组中,这本身就是加 ...

我只是想知道copyPic_2里面那个地方错了啊,为什么会报异常呢?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马