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

© 请备注 中级黑马   /  2014-4-8 15:14  /  1079 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.io.*;

class CopyPic
{
        public static void main(String[] args)
        {
                FileInputStream fis = null;
                FileOutputStream fos = null;

                try
                {
                        fis = new FileInputStream("1.jpg");
                        fos = new FileOutputStream("2.jpg");

                        byte[] buf = new byte[1024];
                        int len = 0;
                        while((len=fis.read())!=-1)
                                fos.write(buf,0,len);
                }
                catch (IOException e)
                {
                        throw new RuntimeException("复制文件失败");
                }
                finally
                {
                        try
                        {
                                if(fos!=null)
                                    fos.close();
                        }
                        catch (IOException e)
                        {
                                throw new RuntimeException("读取关闭失败");
                        }
                        try
                        {
                                if(fis!=null)
                                    fis.close();
                        }
                        catch (IOException e)
                        {
                                throw new RuntimeException("写入关闭失败");
                        }
                }
        }
}


EQ_{A]7UFXXG01}$H8H7@8Q.jpg (22.22 KB, 下载次数: 6)

EQ_{A]7UFXXG01}$H8H7@8Q.jpg

评分

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

查看全部评分

4 个回复

倒序浏览
本帖最后由 chen20134 于 2014-4-8 15:43 编辑
  1. import java.io.*;
  2. class CopyPic
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 FileInputStream fis = null;
  7.                 FileOutputStream fos = null;

  8.                 try
  9.                 {
  10.                         fis = new FileInputStream("1.jpg");
  11.                         fos = new FileOutputStream("2.jpg");

  12.                         byte[] buf = new byte[1024];
  13.                         int len = 0;
  14.                         while((len=fis.read(buf))!=-1)                         //这里把read()改为read(buf)
  15.                         {
  16.                                 fos.write(buf,0,len);
  17.                         }
  18.                 }
  19.                 catch (IOException e)
  20.                 {
  21.                         throw new RuntimeException("复制文件失败");
  22.                 }
  23.                 finally
  24.                 {
  25.                         try
  26.                         {
  27.                                 if(fos!=null)
  28.                                     fos.close();
  29.                         }
  30.                         catch (IOException e)
  31.                         {
  32.                                 throw new RuntimeException("读取关闭失败");
  33.                         }
  34.                         try
  35.                         {
  36.                                 if(fis!=null)
  37.                                     fis.close();
  38.                         }
  39.                         catch (IOException e)
  40.                         {
  41.                                 throw new RuntimeException("写入关闭失败");
  42.                         }
  43.                 }
  44.         }
  45. }
复制代码

FileInputStream 中有三个读入的方法:
int read()         从此输入流中读取一个数据字节。
int read(byte[] b)        从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
int read(byte[] b, int off, int len)          从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。

你的原程序使用的是read()方法,返回的是下一个数据的字节数,所以在write的时候写入的数据也不是存入buf的数据。
加上buf后调用的方法会一次读取1024字节的数据存在buf中。
你也可以参考FileInputStream 中的三个读入的方法进行修改

评分

参与人数 1技术分 +1 收起 理由
ily521125 + 1 很给力!

查看全部评分

回复 使用道具 举报
class Test1
{
        public static void main(String[] args)
        {
                FileInputStream fis = null;
                FileOutputStream fos = null;

                try
                {
                        fis = new FileInputStream("c://users//Administrator//Desktop//1.jpg");
                        fos = new FileOutputStream("c://users//Administrator//Desktop//2.jpg");

                        byte[] buf = new byte[1024];
                       int len = 0;
                        while((len=fis.read(buf))!=-1)   //这个地方你应该每次将读到的字节装入缓冲区buf里,你少了这一步,所以无法打开
                                fos.write(buf,0,len);
                }
                catch (IOException e)
                {
                        e.printStackTrace();
                }
                finally
                {
                        try
                        {
                               if(fos!=null)
                                    fos.close();
                        }
                        catch (IOException e)
                        {
                                throw new RuntimeException("读取关闭失败");
                        }
                        try
                        {
                              if(fis!=null)
                                    fis.close();
                        }
                        catch (IOException e)
                        {
                                throw new RuntimeException("写入关闭失败");
                        }
                }
        }
}

评分

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

查看全部评分

回复 使用道具 举报
恩  后来检查出来 了
回复 使用道具 举报
问题应该出在你指定了buf的大小
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马