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