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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 耿渊博 中级黑马   /  2014-4-10 09:39  /  628 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 耿渊博 于 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. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
枫儿 + 1 神马都是浮云

查看全部评分

2 个回复

倒序浏览
close()的作用是关闭这个流,并且释放这个流所占用的资源
回复 使用道具 举报
close()的作用是关闭此流并释放与此流关联的所有系统资源。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马