Black Horse programmer--java--file cutting and merging

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.