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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 不破大地 中级黑马   /  2013-6-2 21:48  /  1858 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 不破大地 于 2013-6-3 01:42 编辑

public static void main(String[] args)
      {
              FileOutputStream fos=null;
              FileInputStream  fis=null;
              try
              {
                      fos=new FileOutputStream("d:\\www.jpg");
                      fis=new FileInputStream("d:\\1.jpg");
                      byte[] buf=new byte[1024];
                      int len=0;
                      while((len=fis.read(buf))!=-1);
                      {
                               fos.write(buf,0,len);
                      }
              }
              catch(IOException e)
              {
                      throw new RuntimeException("复制文件失败");
              }
              finally
              {
                      try
                      {
                              if(fis!=null)
                                      fis.close();
                      }
                      catch(IOException e)
                  {
                          throw new RuntimeException("读取失败");
                  }
                      try
                      {
                              if(fos!=null)
                                      fos.close();
                      }
                      catch(IOException e)
                  {
                          throw new RuntimeException("写入失败");
                  }
              }
      }
在该程序中,编译能通过,并且有结果,但是复制出来的没有图片内容,并且出现了角标越界的错误
苦思良久不得其解,还请各位师兄师姐指教:
Exception in thread "main" java.lang.IndexOutOfBoundsException        at java.io.FileOutputStream.writeBytes(Native Method)
        at java.io.FileOutputStream.write(FileOutputStream.java:260)
        at iobufffered.piccopy.main(piccopy.java:21)

9 个回复

倒序浏览
  你的数组长度不够, 所以会角标越界,你可以直接用缓冲区,
别自己搞,字节类型的缓冲区稍微麻烦一些  如果真想自己写 ,
你可以去查一下源码,看看 - -!  嘻嘻
回复 使用道具 举报
花开花落总相似 发表于 2013-6-2 22:03
你的数组长度不够, 所以会角标越界,你可以直接用缓冲区,
别自己搞,字节类型的缓冲区稍微麻烦一些  如 ...

哦哦,我的图片不大的啊,只有20k,而且毕老师不是说字节流不需要缓冲吗?
回复 使用道具 举报
不破大地 发表于 2013-6-2 22:08
哦哦,我的图片不大的啊,只有20k,而且毕老师不是说字节流不需要缓冲吗? ...

  我去 怎么可能会没有缓冲区,有的  你等一下 我给你拔一下源码,刚才没仔细看
  你要是  上面的  OutputStream os = null;   里面的定义成 os = new OutputStream(new FileOutputStream(("d:\\www.jpg")); 肯定没错    至于直接定义File这个我还没试过  你等一下
我给敲一下  一会顺便也把缓冲区的代发发给你   
回复 使用道具 举报
我又按照你写的 写了一遍
没有问题 不会报错

import java.io.*;
class CopyImage{

        public static void main(String[]args)throws Exception{
                FileOutputStream fs = new FileOutputStream("sdjf.jpg");
                FileInputStream fis = new FileInputStream("psb.jpg");
                int len  = 0;
                byte [] by = new byte[1];
                while((len = fis.read(by))!=-1){
                        fs.write(by,0,len);
                }
                fs.close();
                fis.close();
        }
}
下面我把缓冲区的源码发给你 你自己去理解 我第一的说法有问题
等一下我也还要看一下
private void fill() throws IOException {       //这个就是源码中判断缓冲区大小的方法 ,怎么可能没有缓冲区呢
        byte[] buffer = getBufIfOpen();
        if (markpos < 0)
            pos = 0;            /* no mark: throw away the buffer */
        else if (pos >= buffer.length)  /* no room left in buffer */
            if (markpos > 0) {  /* can throw away early part of the buffer */
                int sz = pos - markpos;
                System.arraycopy(buffer, markpos, buffer, 0, sz);
                pos = sz;
                markpos = 0;
            } else if (buffer.length >= marklimit) {
                markpos = -1;   /* buffer got too big, invalidate mark */
                pos = 0;        /* drop buffer contents */
            } else {            /* grow buffer */
                int nsz = pos * 2;
                if (nsz > marklimit)
                    nsz = marklimit;
                byte nbuf[] = new byte[nsz];
                System.arraycopy(buffer, 0, nbuf, 0, pos);
                if (!bufUpdater.compareAndSet(this, buffer, nbuf)) {
                    // Can't replace buf if there was an async close.
                    // Note: This would need to be changed if fill()
                    // is ever made accessible to multiple threads.
                    // But for now, the only way CAS can fail is via close.
                    // assert buf == null;
                    throw new IOException("Stream closed");
                }
                buffer = nbuf;
            }
        count = pos;
        int n = getInIfOpen().read(buffer, pos, buffer.length - pos);
        if (n > 0)
            count = n + pos;
    }

    /**
     * See
     * the general contract of the <code>read</code>
     * method of <code>InputStream</code>.
     *
     * @return     the next byte of data, or <code>-1</code> if the end of the
     *             stream is reached.
     * @exception  IOException  if this input stream has been closed by
     *                          invoking its {@link #close()} method,
     *                          or an I/O error occurs.
     * @see        java.io.FilterInputStream#in
     */
    public synchronized int read() throws IOException {
        if (pos >= count) {
            fill();
            if (pos >= count)
                return -1;
        }
        return getBufIfOpen()[pos++] & 0xff;
    }
回复 使用道具 举报
我觉得吧 是你  while((len=fis.read(buf))!=-1); 多了一个分号{:soso_e127:}
回复 使用道具 举报
import java.io.*;
class W
{
       

        /**
         * @param args
         */
        public static void main(String[] args)throws IOException {
                //封装目录
                BufferedInputStream br=new BufferedInputStream(new FileInputStream("d:\\b.jpg"));
                //目的地目录
                BufferedOutputStream bw=new BufferedOutputStream(new FileOutputStream("e:\\a.jpg"));
                //读写文件
                byte[] by=new byte[1024];
                int len=0;
                while((len=br.read(by))!=-1){
                        bw.write(by,0,len);
                        bw.flush();
                }
                bw.close();
                br.close();

        }


}
有异常还是抛吧,写那么多try乱了,抛完后多简单代码。
回复 使用道具 举报
仁兄,以后悠着点,低级错误!
public static void main(String[] args)
      {
              FileOutputStream fos=null;
              FileInputStream  fis=null;
              try
              {
                      fos=new FileOutputStream("d:\\www.jpg");
                      fis=new FileInputStream("d:\\1.jpg");
                      byte[] buf=new byte[1024];
                      int len=0;
                      while((len=fis.read(buf))!=-1);//看到分号了有木有啊!!                      {
                               fos.write(buf,0,len);
                      }
              }
              catch(IOException e)
              {
                      throw new RuntimeException("复制文件失败");
              }
              finally
              {
                      try
                      {
                              if(fis!=null)
                                      fis.close();
                      }
                      catch(IOException e)
                  {
                          throw new RuntimeException("读取失败");
                  }
                      try
                      {
                              if(fos!=null)
                                      fos.close();
                      }
                      catch(IOException e)
                  {
                          throw new RuntimeException("写入失败");
                  }
              }
      }
回复 使用道具 举报
kaka小明 发表于 2013-6-2 23:10
仁兄,以后悠着点,低级错误!
public static void main(String[] args)
      {

是的是的,谢谢仁兄指教,咳,以后要多注意了
回复 使用道具 举报
littlefoxtail 发表于 2013-6-2 22:54
我觉得吧 是你  while((len=fis.read(buf))!=-1); 多了一个分号

咳,纠结了大半天,谢谢哈。。:handshake
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马