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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 学习代码 中级黑马   /  2014-3-31 18:11  /  1315 人查看  /  10 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.io.*;
  2. class Demo
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                  FileInputStream fi= null;
  7.                  FileOutputStream fo= null;
  8.                 try
  9.                 {
  10.                         fi= new FileInputStream("f:\\1.jpg");
  11.                         fo = new FileOutputStream("f:\\2.jpg");
  12.                         char[] arr = new char[1024];
  13.                         int len = 0;
  14.                         while((len= fi.read(arr))!=-1)
  15.                         {
  16.                                 fo.write(arr,0,len);
  17.                         }
  18.                 }
  19.                 catch (Exception e)
  20.                 {
  21.                         throw new RuntimeException("shujuyichang");
  22.                 }
  23.                 finally
  24.                 {
  25.                         try
  26.                         {
  27.                                 if(fi== null)
  28.                                         fi.close();
  29.                         }
  30.                         catch (Exception e)
  31.                         {
  32.                                 throw new RuntimeException("shuchu shibai");
  33.                         }
  34.                         try
  35.                         {
  36.                                 if(fo ==null)
  37.                                         fo.close();
  38.                         }
  39.                         catch (Exception e)
  40.                         {
  41.                                 throw new RuntimeException("xieru shibai");
  42.                         }
  43.                 }
  44.         }
  45. }
复制代码

为什么 会报错啊

评分

参与人数 1技术分 +1 收起 理由
菜小徐 + 1

查看全部评分

10 个回复

倒序浏览
FileInputStream,FileOutputStream 属性字节流派。read(byte[] bts)  read中要用字节数组,不能用字符数组。
write(byte[] bts)一样。
回复 使用道具 举报
    byte[] arr = new byte[1024];  即可  
回复 使用道具 举报
字节流使用字节数组你使用的字符数组,还有关闭流操作时,你的if条件也有错,应该是!=null才关闭
回复 使用道具 举报
从硬盘读取文件,使用FileInputStream,读取到的是字节流数据,是不能用字符数组来存的。 这是常识啊,比如你要拷贝mp3文件什么的,都得用字节流来读取数据的。 还有楼上说的,关闭流操作的时候你的判断条件写错了,应该是fis!=null;和fos!=null;
回复 使用道具 举报
  1. public class CopyPic {
  2.         public static void main(String[] args){
  3.                 FileOutputStream fos = null;
  4.                 FileInputStream fis = null;
  5.                 try{       
  6.                         fis = new FileInputStream("D:\\1.jpg");
  7.                         fos = new FileOutputStream("D:\\2.jpg");
  8.                        
  9.                         byte[] by = new byte[1024];
  10.                         int len = 0;
  11.                         while((len=fis.read(by))!=-1){
  12.                                 fos.write(by,0,len);
  13.                         }
  14.                        
  15.                 }catch(IOException e){
  16.                         throw new RuntimeException("复制文件失败");
  17.                 }finally{
  18.                         try{
  19.                                 if(fis!=null){
  20.                                         fis.close();
  21.                                 }
  22.                         }catch(IOException e){
  23.                                 throw new RuntimeException("读取关闭失败失败");
  24.                         }
  25.                         try{
  26.                                 if(fos!=null){
  27.                                         fos.close();
  28.                                 }
  29.                         }catch(IOException e){
  30.                                 throw new RuntimeException("写入关闭失败失败");
  31.                         }
  32.                 }
  33.                
  34.         }
  35. }       
复制代码

评分

参与人数 1技术分 +1 收起 理由
菜小徐 + 1

查看全部评分

回复 使用道具 举报
FileInputStream 用于读取诸如图像数据之类的原始字节流
FileOutputStream 用于写入诸如图像数据之类的原始字节流
回复 使用道具 举报
有两个错误,看下面代码。
  1. import java.io.*;
  2. class Demo
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                  FileInputStream fi= null;
  7.                  FileOutputStream fo= null;
  8.                 try
  9.                 {
  10.                         fi= new FileInputStream("f:\\1.jpg");
  11.                         fo = new FileOutputStream("f:\\2.jpg");
  12.                         //1.这里的char改成byte.
  13.                         byte[] arr = new byte[1024];
  14.                         int len = 0;
  15.                         while((len= fi.read(arr))!=-1)
  16.                         {
  17.                                 fo.write(arr,0,len);
  18.                         }
  19.                 }
  20.                 catch (Exception e)
  21.                 {
  22.                         throw new RuntimeException("shujuyichang");
  23.                 }
  24.                 finally
  25.                 {
  26.                         try
  27.                         {
  28.                                         //2."=="改成"!=",下面fo==null也是改成fo!=null
  29.                                 if(fi!= null)
  30.                                         fi.close();
  31.                         }
  32.                         catch (Exception e)
  33.                         {
  34.                                 throw new RuntimeException("shuchu shibai");
  35.                         }
  36.                         try
  37.                         {
  38.                                 if(fo !=null)
  39.                                         fo.close();
  40.                         }
  41.                         catch (Exception e)
  42.                         {
  43.                                 throw new RuntimeException("xieru shibai");
  44.                         }
  45.                 }
  46.         }
  47. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
菜小徐 + 1

查看全部评分

回复 使用道具 举报
           try
                {
                        fi= new FileInputStream("f:\\1.jpg");
                        fo = new FileOutputStream("f:\\2.jpg");
                        char[] arr = new char[1024];---------------->第一次错误 byte[] by = new byte[1024];
                        int len = 0;
                        while((len= fi.read(arr))!=-1)
                        {
                                fo.write(arr,0,len);
                        }
                }
                catch (Exception e)
                {
                        throw new RuntimeException("shujuyichang");
                }
                finally
                {
                        try
                        {
                                if(fi== null)----------------〉第二次  if(fi!= null)
                                        fi.close();
                        }
                        catch (Exception e)
                        {
                                throw new RuntimeException("shuchu shibai");
                        }
                        try
                        {
                                if(fo ==null)----------------〉 第三次   if(fo !=null)  
                                        fo.close();
                        }
                        catch (Exception e)
                        {
                                throw new RuntimeException("xieru shibai");
                        }
                }
        }
}

评分

参与人数 1技术分 +1 收起 理由
菜小徐 + 1

查看全部评分

回复 使用道具 举报

  1. char是字符类型,不适用于字节流,而图片是字节流的,所以用byte,把这里改完就好了
  2.                 try
  3.                 {
  4.                         fi= new FileInputStream("1.bmp");
  5.                         fo = new FileOutputStream("2.jpg");
  6.                         byte[] arr = new byte[1024]; // byte类型
  7.                         int len = 0;
  8.                         while((len= fi.read(arr))!=-1)
  9.                         {
  10.                                 fo.write(arr,0,len);
  11.                         }
  12.                 }
  13.                
复制代码
回复 使用道具 举报
import java.io.*;
class Demo
{
        public static void main(String[] args)
        {
                 FileInputStream fi= null;
                 FileOutputStream fo= null;
                try
                {
                        fi= new FileInputStream("f:\\1.jpg");
                        fo = new FileOutputStream("f:\\2.jpg");
                        //char[] arr = new char[1024];
                                                //int read(byte[] b) FileInputStream中的方法数组不是字符数组 是字节数组
                                                byte[] arr =new byte[1024];
                        int len = 0;
                        while((len= fi.read(arr))!=-1)
                        {
                                fo.write(arr,0,len);
                        }
                }
                catch (Exception e)
                {
                        throw new RuntimeException("shujuyichang");
                }
                finally
                {
                        try
                        {
                                if(fi== null)
                                        fi.close();
                        }
                        catch (Exception e)
                        {
                                throw new RuntimeException("shuchu shibai");
                        }
                        try
                        {
                                if(fo ==null)
                                        fo.close();
                        }
                        catch (Exception e)
                        {
                                throw new RuntimeException("xieru shibai");
                        }
                }
        }
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马