黑马程序员技术交流社区

标题: 视频教程中的一段代码修改成这样更合适吧?[已解决] [打印本页]

作者: 王金科    时间: 2012-8-19 23:42
标题: 视频教程中的一段代码修改成这样更合适吧?[已解决]
本帖最后由 王金科 于 2012-8-23 15:37 编辑
  1. 视频中的源代码
  2. /*
  3. 文件切割和合并
  4. */
  5. import java.util.*;
  6. import java.io.*;
  7. class  SplitFile
  8. {
  9.         public static void main(String[] args) throws IOException
  10.         {
  11.                 //splitFile();
  12.                 merge();
  13.         }

  14.         //合并文件
  15.         public static void merge() throws IOException
  16.         {
  17.                 ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();

  18.                 for(int x=1;x<=6;x++)
  19.                 {
  20.                         al.add(new FileInputStream("F:\\split\\"+x+".part"));
  21.                 }

  22.                 final Iterator<FileInputStream> it = al.iterator();

  23.                 Enumeration<FileInputStream> en = new Enumeration<FileInputStream>()
  24.                 {
  25.                         public boolean hasMoreElements()
  26.                         {
  27.                                 return it.hasNext();
  28.                         }
  29.                         public FileInputStream nextElement()
  30.                         {
  31.                                 return it.next();
  32.                         }
  33.                 };
  34.                 SequenceInputStream sis = new SequenceInputStream(en);
  35.                 FileOutputStream fos = new FileOutputStream("F:\\2.rm");
  36.                 byte[] buf = new byte[1024*1024*20];
  37.                 int len = 0;
  38.                 while((len=sis.read(buf))!=-1)
  39.                 {
  40.                         fos.write(buf,0,len);
  41.                 }
  42.                 fos.close();
  43.                 sis.close();
  44.         }

  45.         //切割文件

  46.         public static void splitFile()throws IOException
  47.         {
  48.                 FileInputStream fis = new FileInputStream("F:\\1.rm");
  49.                 FileOutputStream fos = null;
  50.                 byte[] buf = new byte[1024*1024*20];
  51.                 int len = 0;
  52.                 int count = 1;
  53.                 while((len=fis.read(buf))!=-1)
  54.                 {
  55.                         fos = new FileOutputStream("F:\\split\\"+(count++)+".part");
  56.                         fos.write(buf,0,len);
  57.                         fos.close();
  58.                 }
  59.                 fis.close();
  60.         }
  61. }
复制代码
for(int x=1;x<=6;x++)
                {
                        al.add(new FileInputStream("F:\\split\\"+x+".part"));
                }
这里我们是知道碎片文件有6个的情况下才定义x<=6
  1. package cn.study.file;

  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.SequenceInputStream;
  7. import java.util.ArrayList;
  8. import java.util.Enumeration;
  9. import java.util.Iterator;


  10. public class SplitFile {

  11.         /**
  12.          * @param args
  13.          */
  14.         public static void main(String[] args) throws IOException
  15.         {
  16.                 //splitFile();
  17.                 merge();
  18.         }

  19.         //合并文件
  20.                 public static void merge() throws IOException
  21.                 {
  22.                         ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
  23.                         File dir = new File("F:\\split\\");
  24.                         File[] files = dir.listFiles();
  25.                         for(int x=1;x<=files.length;x++)
  26.                         {
  27.                                 al.add(new FileInputStream("F:\\split\\"+x+".part"));
  28.                         }

  29.                         final Iterator<FileInputStream> it = al.iterator();

  30.                         Enumeration<FileInputStream> en = new Enumeration<FileInputStream>()
  31.                         {
  32.                                 public boolean hasMoreElements()
  33.                                 {
  34.                                         return it.hasNext();
  35.                                 }
  36.                                 public FileInputStream nextElement()
  37.                                 {
  38.                                         return it.next();
  39.                                 }
  40.                         };
  41.                         SequenceInputStream sis = new SequenceInputStream(en);
  42.                         FileOutputStream fos = new FileOutputStream("F:\\2.rmvb");
  43.                         byte[] buf = new byte[1024*1024*20];
  44.                         int len = 0;
  45.                         while((len=sis.read(buf))!=-1)
  46.                         {
  47.                                 fos.write(buf,0,len);
  48.                         }
  49.                         fos.close();
  50.                         sis.close();
  51.                 }

  52.                 //切割文件

  53.                 public static void splitFile()throws IOException
  54.                 {
  55.                         FileInputStream fis = new FileInputStream("F:\\1.rmvb");
  56.                         FileOutputStream fos = null;
  57.                         byte[] buf = new byte[1024*1024*20];
  58.                         int len = 0;
  59.                         int count = 1;
  60.                         while((len=fis.read(buf))!=-1)
  61.                         {
  62.                                 fos = new FileOutputStream("F:\\split\\"+(count++)+".part");
  63.                                 fos.write(buf,0,len);
  64.                                 fos.close();
  65.                         }
  66.                         fis.close();
  67.                 }
  68. }
复制代码
我修改后的代码如下这样子是不是更贴近实际一点?



作者: 赵俊杰    时间: 2012-8-20 01:09
{:soso__11993763108002953784_3:}
作者: 邓超军    时间: 2012-8-20 07:38
File[] files = dir.listFiles();

                        for(int x=1;x<=files.length;x++)

                        {

                                al.add(new FileInputStream("F:\\split\\"+x+".part"));

                        }


这一段代码很容易出现一个问题就是F:\split\中可能不仅仅存在.part文件,这样x的值就会大于part文件的数量,就会出现错误。可以修改成如下代码:
  1. public static void merge() throws IOException
  2.     {
  3.             ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
  4.             File dir = new File("F:\\split\\");
  5.             File[] files = dir.listFiles();

  6.             int count=0;
  7.             
  8.             for(File f:files)
  9.             {
  10.                     String str=f.toString();
  11.                     if(str.endsWith(".part"))
  12.                             count++;
  13.             }
  14.             for(int x=1;x<=count;x++)
  15.             {
  16.                     al.add(new FileInputStream("F:\\split\\"+x+".part"));
  17.             }

  18.             final Iterator<FileInputStream> it = al.iterator();

  19.             Enumeration<FileInputStream> en = new Enumeration<FileInputStream>()
  20.             {
  21.                     public boolean hasMoreElements()
  22.                     {
  23.                             return it.hasNext();
  24.                     }
  25.                     public FileInputStream nextElement()
  26.                     {
  27.                             return it.next();
  28.                     }
  29.             };
  30.             SequenceInputStream sis = new SequenceInputStream(en);
  31.             FileOutputStream fos = new FileOutputStream("F:\\2.rm");
  32.             byte[] buf = new byte[1024*1024*20];
  33.             int len = 0;
  34.             while((len=sis.read(buf))!=-1)
  35.             {
  36.                     fos.write(buf,0,len);
  37.             }
  38.             fos.close();
  39.             sis.close();
  40.     }
复制代码

作者: 王金科    时间: 2012-8-20 18:20
邓超军 发表于 2012-8-20 07:38
这一段代码很容易出现一个问题就是F:\split\中可能不仅仅存在.part文件,这样x的值就会大于part文件的数 ...

老兄,你这个太好了:victory:
作者: goodday    时间: 2018-5-12 17:46
好厉害aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2