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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© papercup 中级黑马   /  2014-4-4 18:24  /  930 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

今天看了毕老师的视频,写了一个文件合并的demo,可是文件没办法打开,请大神来帮我看下。。
  1. import java.io.*;
  2. import java.util.*;
  3. public class SplitFileDemo {
  4.         //main 函数
  5.         public static void main(String[] args) throws IOException{
  6.         //        splitFile();
  7.                 mergeFile();
  8.         }

  9.         public static void mergeFile() throws IOException{
  10.                 //用arraylist集合存放输入流
  11.                 ArrayList<FileInputStream> al = new ArrayList<FileInputStream> ();
  12.                 //将3个碎片文件放入al中
  13.                 for(int i = 1; i<=3;i++){
  14.                         al.add(new FileInputStream("e:\\split\\"+i+".part"));
  15.                 }
  16.                 //定义一个迭代器 it
  17.                 final Iterator<FileInputStream> it = al.iterator();
  18.                 //用Enumeration 对象对输入流进行枚举
  19.                 Enumeration<FileInputStream> en = new Enumeration<FileInputStream>(){
  20.                         public boolean hasMoreElements(){
  21.                                 return it.hasNext();
  22.                         }
  23.                         public FileInputStream nextElement(){
  24.                                 return it.next();
  25.                         }
  26.                 };
  27.                 //对输入流进行合并
  28.                 SequenceInputStream sis = new SequenceInputStream(en);
  29.                 //定义输出流对象和 输出文件路径
  30.                 FileOutputStream fos = new FileOutputStream("e:\\split\\a.bmp");
  31.                
  32.                 byte[] buf = new byte[1024];
  33.                 int len = 0;
  34.                 //输出流进行写文件操作
  35.                 while((len = sis.read())!=-1){
  36.                         fos.write(buf,0,len);
  37.                 }
  38.                 //流关闭
  39.                 fos.close();
  40.                 sis.close();
  41.         }
  42. }
复制代码



评分

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

查看全部评分

4 个回复

倒序浏览
你没有读到byte数组里面去
while((len = sis.read())!=-1){
回复 使用道具 举报
读的时候有错误
  1. import java.io.*;
  2. import java.util.*;
  3. public class SplitFileDemo {
  4.         //main 函数
  5.         public static void main(String[] args) throws IOException{
  6.         //        splitFile();
  7.                 mergeFile();
  8.         }

  9.         public static void mergeFile() throws IOException{
  10.                 //用arraylist集合存放输入流
  11.                 ArrayList<FileInputStream> al = new ArrayList<FileInputStream> ();
  12.                 //将3个碎片文件放入al中
  13.                 for(int i = 1; i<=3;i++){
  14.                         al.add(new FileInputStream("e:\\split\\"+i+".part"));
  15.                 }
  16.                 //定义一个迭代器 it
  17.                 final Iterator<FileInputStream> it = al.iterator();
  18.                 //用Enumeration 对象对输入流进行枚举
  19.                 Enumeration<FileInputStream> en = new Enumeration<FileInputStream>(){
  20.                         public boolean hasMoreElements(){
  21.                                 return it.hasNext();
  22.                         }
  23.                         public FileInputStream nextElement(){
  24.                                 return it.next();
  25.                         }
  26.                 };
  27.                 //对输入流进行合并
  28.                 SequenceInputStream sis = new SequenceInputStream(en);
  29.                 //定义输出流对象和 输出文件路径
  30.                 FileOutputStream fos = new FileOutputStream("e:\\split\\a.bmp");
  31.                
  32.                 byte[] buf = new byte[1024];
  33.                 int len = 0;
  34.                 //输出流进行写文件操作
  35.                 while((len = sis.read(buf))!=-1){
  36.                         fos.write(buf,0,len);
  37.                 }
  38.                 //流关闭
  39.                 fos.close();
  40.                 sis.close();
  41.         }
  42. }
复制代码

评分

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

查看全部评分

回复 使用道具 举报
osully 发表于 2014-4-4 18:28
你没有读到byte数组里面去
while((len = sis.read())!=-1){

恩,就是这样,你好快啊。。。。
回复 使用道具 举报
楼上几个好快啊,问题已解决,多谢了~
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马