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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张振纲 中级黑马   /  2012-8-10 22:41  /  1072 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.io.*;
  2. class MyBufferedInputStream
  3. {
  4.         private InputStream is;
  5.         private int count=0;
  6.         private int n=0;
  7.         private byte[] date = new byte[1024];
  8.         MyBufferedInputStream(InputStream is)
  9.         {
  10.                 this.is=is;
  11.         }

  12.         public int myRead() throws IOException
  13.         {
  14.                 if(count==0) //如果数据被取空了就重新取数据
  15.                 {
  16.                         count=is.read(date); //从文件中读出数据并且存入字节数组中
  17.                         if (count==-1)
  18.                                 return -1;
  19.                         byte b = date[n]; //从数组中取出一个字节
  20.                         count--;
  21.                         n++;
  22.                         return b&255;  //返回这个字节

  23.                        
  24.                 }
  25.                 if (count>0) //如果有数据,就继续取
  26.                 {
  27.                        
  28.                         byte b = date[n]; //从数组中取出一个字节
  29.                         count--;
  30.                         n++;
  31.                         return b&255;  //返回这个字节
  32.                 }
  33.                 else
  34.                         return -1;
  35.         }

  36.         public void myClose() throws IOException
  37.         {
  38.                 is.close();
  39.         }
  40. }
  41. class Buffered
  42. {
  43.         public static void main(String[] args)  throws IOException
  44.         {
  45.                 FileInputStream fis = new FileInputStream("6PU02J4D2IIQ0031.jpg");
  46.                 FileOutputStream fos = new FileOutputStream("111copy.jpg");
  47.                
  48.                 MyBufferedInputStream mbis = new MyBufferedInputStream(fis);
  49.                
  50.                 int c = 0;

  51.                 while ((c = mbis.myRead())!=-1)  //判断流中是否还有数据
  52.                 {
  53.                         fos.write(c);
  54.                 }
  55.        
  56.                

  57.                 fos.close();

  58.                 mbis.myClose();

  59.         }
  60. }
复制代码
为什么会发现数组越界异常
拷贝文本的时候没有问题,拷贝图片为什么会出现问题
二楼上图

6 个回复

倒序浏览

这是报错图
回复 使用道具 举报
这是要拷贝的图片

6PU02J4D2IIQ0031.jpg (171.76 KB, 下载次数: 4)

6PU02J4D2IIQ0031.jpg
回复 使用道具 举报
为什么没人知道呢?
回复 使用道具 举报
import java.io.*;
class MyBufferedInputStream
{
        private InputStream is;
        private int count=0;
        private int n=0;
        private byte[] date = new byte[2];
        MyBufferedInputStream(InputStream is)
        {
                this.is=is;
        }

        public int myRead() throws IOException
        {
                if(count==0) //如果数据被取空了就重新取数据
                {
                        n = 0;//n需要重置为0,如果不重置为0,一直加上去,肯定越界啊
                        count=is.read(date); //从文件中读出数据并且存入字节数组中
                        if (count==-1){
                                return -1;
                        }
                        byte b = date[n]; //从数组中取出一个字节,这时候应该又从角标0开始读取数据
                        count--;
                        n++;
                        return b&255;  //返回这个字节


                }
                else  if (count>0) //如果有数据,就继续取
                {

                        byte b = date[n]; //从数组中取出一个字节
                        count--;
                        n++;
                        return b&255;  //返回这个字节
                }
                else
                        return -1;
        }

        public void myClose() throws IOException
        {
                is.close();
        }
}
public class Test
{
        public static void main(String[] args)  throws IOException
        {
                FileInputStream fis = new FileInputStream("F:\\11.jpg");
                FileOutputStream fos = new FileOutputStream("E:\\22.jpg");

                MyBufferedInputStream mbis = new MyBufferedInputStream(fis);

                int c = 0;

                while ((c = mbis.myRead())!=-1)  //判断流中是否还有数据
                {
                        fos.write(c);
                }



                fos.close();

                mbis.myClose();

        }
}
回复 使用道具 举报
杨文宇 发表于 2012-8-10 23:19
import java.io.*;
class MyBufferedInputStream
{

太感谢了,终于找到原因了
回复 使用道具 举报
张振纲 发表于 2012-8-10 23:47
太感谢了,终于找到原因了

没事,应该的,那些小错误有的时候还是比较难找的:handshake
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马