黑马程序员技术交流社区
标题:
切割文件中的疑问
[打印本页]
作者:
耿渊博
时间:
2014-4-10 09:39
标题:
切割文件中的疑问
本帖最后由 耿渊博 于 2014-4-10 15:46 编辑
看到毕老师视频中讲的切割文件的时候,每切割一次就close()一次输出流,一定要这样吗?
package TestDemo;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
public class SplitFileDemo {
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
SplitFile();
// megre();
}
public static void megre()throws IOException{
ArrayList<FileInputStream> a1 = new ArrayList<FileInputStream>();
for(int x=1;x<=5;x++){
a1.add(new FileInputStream("F:\\Eclipse\\exam\\splitfile\\"+x+".part"));
}
final Iterator<FileInputStream> it = a1.iterator();
Enumeration<FileInputStream> en = new Enumeration<FileInputStream>(){
public boolean hasMoreElements(){
return it.hasNext();
}
public FileInputStream nextElement(){
return it.next();
}
};
SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream("F:\\Eclipse\\exam\\splitfile\\1.jpg");
byte[] buf = new byte[1024];
int len = 0;
while((len=sis.read(buf))!=-1){
fos.write(buf,0,len);
}
fos.close();
sis.close();
}
public static void SplitFile()throws IOException{
FileInputStream fis = new FileInputStream("D:\\用户目录\\Pictures\\新建文件夹\\1.jpg");
FileOutputStream fos = null;
byte[] buf = new byte[1024*1024];
int len = 0;
int count = 1;
while((len=fis.read(buf))!=-1){
fos = new FileOutputStream("F:\\Eclipse\\exam\\splitfile\\"+(count++)+".part");
fos.write(buf,0,len);
fos.close();
}
System.out.println("打印");
fis.close();
}
}
复制代码
作者:
491138002
时间:
2014-4-10 10:43
close()的作用是关闭这个流,并且释放这个流所占用的资源
作者:
東少
时间:
2014-4-10 14:36
close()的作用是关闭此流并释放与此流关联的所有系统资源。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2