<span style= "FONT-SIZE:18PX;" >package Com.io.other.split;import Java.io.file;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.util.properties;public class Splitfiletest {/** * file cutter. * One read stream, corresponding to multiple output streams. And the resulting fragmented file has an ordered number * @param args * @throws ioexception */private static final int default_size=1024*2;public static void Ma In (string[] args) throws IOException {file Srcfile=new file ("Chen\\11.avi"); File Destdir=new file ("Chen\\partfiles"); Splitfile (Srcfile,destdir);} /** * * @param srcfile * @throws ioexception */public static void Splitfile (File srcfile,file destDir) throws Ioexceptio n {if (!srcfile.exists ()) {throw new RuntimeException (Srcfile.getabsolutepath () + ", source file does not exist");} if (!destdir.exists ()) {destdir.mkdirs ();} 1. Define a source file FileInputStream fis=new FileInputStream (srcfile);//2, create purpose reference FileOutputStream FOS=NULL;//3, Create a buffer to store data byte[] buf=new byte[default_size];int len=0;int count=0;while (leN=fis.read (BUF))!=-1) {//create an output stream and clear the file to be written to files Partfile=new (DestDir, (++count) + ". Part"); Fos=new FileOutputStream (partfile); Fos.write (buf, 0, Len); Fos.close ();} When producing fragmented files, you need to generate a configuration file that records the number of fragments and the key value information stored in the source file name//partfile=n;filename=11.avi//configuration file. Use the Properties collection Properties Prop=new properties ();p rop.setproperty ("Partcount", integer.tostring (count)); Prop.setproperty ("filename", srcfile.getname ()); File Confile=new file (DestDir, (++count) + ". Properties"), Fos=new FileOutputStream (confile);//Close Resource fos.close (); Fis.close ();}} </span>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The input and output stream in the javase---a read stream that corresponds to multiple output streams. And the resulting fragmented files are numbered sequentially.