文件的切割与合并

             
[java] view plain copy print ?
  1. package com.io.properties;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.SequenceInputStream;  
  8. import java.util.Enumeration;  
  9. import java.util.Vector;  
  10.   
  11. public class SplitFile {  
  12.     public static void main(String[] args) {  
  13.         try {  
  14.             //split();   
  15.             heBing();  
  16.         } catch (IOException e) {  
  17.             e.printStackTrace();  
  18.         }  
  19.     }  
  20.     /* 
  21.      * 合并多个文件成一个文件 
  22.      */  
  23.     public static void heBing() throws IOException{  
  24.         Vector<FileInputStream> v=new Vector<FileInputStream>();  
  25.         v.add(new FileInputStream("g:/java/1.part"));  
  26.         v.add(new FileInputStream("g:/java/2.part"));  
  27.         v.add(new FileInputStream("g:/java/3.part"));  
  28.         v.add(new FileInputStream("g:/java/4.part"));  
  29.         Enumeration<FileInputStream>en=v.elements();  
  30.         SequenceInputStream sis=new SequenceInputStream(en);  
  31.         int num=0;  
  32.         FileOutputStream fos=new FileOutputStream("g:/java/hebing.mp3");  
  33.         byte[]b=new byte[1024];  
  34.         while(-1!=(num=sis.read(b))){  
  35.             fos.write(b,0,num);  
  36.             fos.flush();  
  37.         }  
  38.         fos.close();  
  39.         sis.close();  
  40.     }  
  41.       
  42.     /* 
  43.      * 切割文件,把一个文件按指定大小进行切割,分割成多个文件 
  44.      */  
  45.     public static void split() throws IOException{  
  46.         FileInputStream fis=new FileInputStream(new File("g:/java/a.mp3"));  
  47.         FileOutputStream fos=null;  
  48.         byte[]b=new byte[1024*1024*5];//大小是5M   
  49.         int num=0;  
  50.         int count=1;  
  51.         while(-1!=(num=fis.read(b))){  
  52.             fos=new FileOutputStream(new File("g:/java/"+ count++ +".part"));  
  53.             fos.write(b,0,num);  
  54.             fos.close();  
  55.         }  
  56.         fis.close();  
  57.       
  58.     }  
  59. }  
package com.io.properties;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;

public class SplitFile {
	public static void main(String[] args) {
		try {
			//split();
			heBing();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/*
	 * 合并多个文件成一个文件
	 */
	public static void heBing() throws IOException{
		Vector<FileInputStream> v=new Vector<FileInputStream>();
		v.add(new FileInputStream("g:/java/1.part"));
		v.add(new FileInputStream("g:/java/2.part"));
		v.add(new FileInputStream("g:/java/3.part"));
		v.add(new FileInputStream("g:/java/4.part"));
		Enumeration<FileInputStream>en=v.elements();
		SequenceInputStream sis=new SequenceInputStream(en);
		int num=0;
		FileOutputStream fos=new FileOutputStream("g:/java/hebing.mp3");
		byte[]b=new byte[1024];
		while(-1!=(num=sis.read(b))){
			fos.write(b,0,num);
			fos.flush();
		}
		fos.close();
		sis.close();
	}
	
	/*
	 * 切割文件,把一个文件按指定大小进行切割,分割成多个文件
	 */
	public static void split() throws IOException{
		FileInputStream fis=new FileInputStream(new File("g:/java/a.mp3"));
		FileOutputStream fos=null;
		byte[]b=new byte[1024*1024*5];//大小是5M
		int num=0;
		int count=1;
		while(-1!=(num=fis.read(b))){
			fos=new FileOutputStream(new File("g:/java/"+ count++ +".part"));
			fos.write(b,0,num);
			fos.close();
		}
		fis.close();
	
	}
}

                                         

你可能感兴趣的:(文件的切割与合并)