Input/output stream in javaSE-one read stream corresponds to multiple output streams. The generated part files are numbered in sequence.
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 corresponds to multiple output streams. The generated part files are numbered in an orderly manner * @ param args * @ throws IOException */private static final int DEFAULT_SIZE = 1024*2; public static void main (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 IOException {if (! SrcFile. exists () {throw new RuntimeException (srcFile. getAbsolutePath () +, the source file does not exist);} if (! DestDir. exists () {destDir. mkdirs ();} // 1. Define a source file, FileInputStream (FS) = new FileInputStream (srcFile); // 2. Create a target 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 = Fi. read (buf ))! =-1) {// create the output stream and specify the File to be written in File partFile = new File (destDir, (++ count) +. part); fos = new FileOutputStream (partFile); fos. write (buf, 0, len); fos. close () ;}// when a part file is generated, a configuration file must be generated to record the number of parts and the source file name // partfile = n; filename = 11.avi// key value information stored in the configuration file. Use the Properties set Properties prop = new Properties (); prop. setProperty (partcount, Integer. toString (count); prop. setProperty (filename, srcFile. getName (); File conFile = new File (destDir, (++ count) +. properties); fos = new FileOutputStream (conFile); // close the resource fos. close (); FCM. close ();}}