黑马程序员技术交流社区

标题: 切割文件中的疑问 [打印本页]

作者: 耿渊博    时间: 2014-4-10 09:39
标题: 切割文件中的疑问
本帖最后由 耿渊博 于 2014-4-10 15:46 编辑

看到毕老师视频中讲的切割文件的时候,每切割一次就close()一次输出流,一定要这样吗?
  1. package TestDemo;

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



  9. public class SplitFileDemo {

  10.         public static void main(String[] args)throws IOException {
  11.                 // TODO Auto-generated method stub
  12.                 SplitFile();
  13. //                megre();
  14.         }

  15.         
  16.         public static void megre()throws IOException{
  17.                 ArrayList<FileInputStream> a1 = new ArrayList<FileInputStream>();
  18.                
  19.                 for(int x=1;x<=5;x++){
  20.                         a1.add(new FileInputStream("F:\\Eclipse\\exam\\splitfile\\"+x+".part"));
  21.                 }
  22.                
  23.                 final Iterator<FileInputStream> it = a1.iterator();
  24.                
  25.                 Enumeration<FileInputStream> en = new Enumeration<FileInputStream>(){
  26.                         public boolean hasMoreElements(){
  27.                                 return it.hasNext();
  28.                         }
  29.                         public FileInputStream nextElement(){
  30.                                 return it.next();
  31.                         }
  32.                 };
  33.                
  34.                 SequenceInputStream sis = new SequenceInputStream(en);
  35.                
  36.                 FileOutputStream fos = new FileOutputStream("F:\\Eclipse\\exam\\splitfile\\1.jpg");
  37.                
  38.                 byte[] buf = new byte[1024];
  39.                
  40.                 int len = 0;
  41.                 while((len=sis.read(buf))!=-1){
  42.                         fos.write(buf,0,len);
  43.                 }
  44.                 fos.close();
  45.                 sis.close();
  46.                
  47.         }
  48.         
  49.         public static void SplitFile()throws IOException{
  50.                 FileInputStream fis = new FileInputStream("D:\\用户目录\\Pictures\\新建文件夹\\1.jpg");
  51.                
  52.                 FileOutputStream fos = null;
  53.                
  54.                 byte[] buf = new byte[1024*1024];
  55.                
  56.                 int len = 0;
  57.                 int count = 1;
  58.                 while((len=fis.read(buf))!=-1){
  59.                         fos = new FileOutputStream("F:\\Eclipse\\exam\\splitfile\\"+(count++)+".part");
  60.                         fos.write(buf,0,len);
  61.                         fos.close();
  62.                 }
  63.                 System.out.println("打印");
  64.                 fis.close();
  65.         }

  66. }
复制代码


作者: 491138002    时间: 2014-4-10 10:43
close()的作用是关闭这个流,并且释放这个流所占用的资源
作者: 東少    时间: 2014-4-10 14:36
close()的作用是关闭此流并释放与此流关联的所有系统资源。




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