cutting and merging of files
Import Java.io.bufferedinputstream;import Java.io.bufferedoutputstream;import Java.io.file;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.io.sequenceinputstream;import java.util.ArrayList; Import Java.util.enumeration;import Java.util.vector;public class Filespliedemos {public static void main (string[] args ) throws Exception {file Thereadpath = new File ("c:\\users\\administrator\\desktop\\ Test folder \ \ Feng Dance nine days. mp3"); String Theoutpath = "c:\\users\\administrator\\desktop\\ Test folder \\abc\\";//Call a custom method to cut a picture file Spliefile (Thereadpath, Theoutpath,new file (". mp3"));//Call a custom method to merge these segmented files into string themergerpathout = "c:\\users\\administrator\\desktop\\ Test folder \\abCOPY\\ "; String themergerinputpath= "c:\\users\\administrator\\desktop\\ Test folder \\abc\\"; String Theoutformat = "Mergercopy.mp3"; Mergerfile (Themergerinputpath,themergerpathout,theoutformat);} private static void Mergerfile (String themergerinputpAth,string themergerpathout, String Theoutformat) throws Exception {//TODO auto-generated method stubfile fil = new File ( Themergerpathout); if (!) ( Fil.isdirectory ())) {Fil.mkdirs ();} Encapsulates the file source to the object file File = new file (themergerinputpath);//Get all blocks of file file[] names = File.listfiles ();//Create a container, Store all file names vector<fileinputstream> v = new vector<fileinputstream> (); for (int x=0;x<names.length;x++) { V.add (New FileInputStream (Names[x])); Enumeration<fileinputstream> en = v.elements ();//Merging multiple Stream objects Sequenceinputstream sis = new Sequenceinputstream (en); /Create output Stream object Bufferedoutputstream bos = new Bufferedoutputstream (new FileOutputStream (Themergerpathout+theoutformat)); int len = 0;byte[] B = new Byte[1024*1024];while ((Len=sis.read (b))!=-1) {bos.write (b); Bos.flush ();}} private static void Spliefile (file file,string thepath,file format) {//*//byte-stream required to cut the files object, and add buffer technology to improve efficiency System.out.println ("Cut file function start"); File fil = new file (thepath); Fil.isdirectory ())) {Fil.mkdirs ();} Bufferedinputstream bis = null; BufferEdoutputstream BOS = Null;int Number = 0;try {bis = new Bufferedinputstream (new FileInputStream (file));//initial output path byte[] B = New byte[1024*1024]; int len = 0; while (len = Bis.read (b))!=-1) {number++; BOS = new Bufferedoutputstream (new FileOutputStream (Thepath+number+format)); Bos.write (b); Bos.flush (); }} catch (FileNotFoundException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//TODO Auto-generated catch Blocke.printstacktrace ();}}}
Black Horse programmer--java--file cutting and merging