Several ways to merge files that are developed by Android

Source: Internet
Author: User

Here are a few ways to merge files, and combine AMR files to illustrate the process of merging files. the file header in AMR format is 6 bytes, so the file header is subtracted from files other than the first file when the file is merged.

Note: different file header is not the same, so at the time of merging according to different files corresponding minus the file header of the merged file.

Step one: Get the files to be merged and create the files saved after merging

/** is used to hold a collection of files to be merged **/list<file>tempfiles=new arraylist<file> ();/** merged files **/file finalfile;
/** * Create files for merging * @param istempfile is a temporary file * @return soundfile file * */private file GETF Ile (Boolean istempfile) {//TODO auto-generated method Stubfinalfile=null;if (! Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {LOG.W ("waring", "detected that your phone is not plugged into an SD card, Please insert SD and try again! ");} Gets the system's 24-hour time as file name (HH is 24-hour, HH is 12-hour) SimpleDateFormat simpledateformat=new SimpleDateFormat (" Yyyy-mm-dd-hh-mm-ss ", Locale.getdefault ()); String Filename=simpledateformat.format (New Date ()) + ". Amr"; if (istempfile) {///if it is a temporary file filename= "temp" +filename;} try {file parentfile= new File (Environment.getexternalstoragedirectory (). Getcanonicalfile () + "/" + "Recorder"); Parentfile.exists () | | Parentfile==null) {//If the directory does not exist Parentfile.mkdirs ();//Create parentfile directory}finalfile=new File (Parentfile, fileName);} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} return finalfile;} 

Step Two: Merge files

Way one: Through FileOutputStream, and FileInputStream Way

/** * Merge multiple files with FileOutputStream, FileInputStream mode * and delete the original file * */public void MergeFiles1 () {//TODO auto-generated Metho D stubif (Tempfiles.isempty ()) return;//if not recorded yet, do not merge file Realfile=getfile (false); try {FileOutputStream fos=new FileOutputStream (Realfile); for (int i = 0; i < tempfiles.size (); i++) {//Traversal Tempfiles collection, merging all temporary files FileInputStream fis= New FileInputStream (Tempfiles.get (i)); byte[] tmpbytes = new byte[fis.available ()];int length = tmpbytes.length;//file length/ /header file if (i==0) {while (Fis.read (tmpbytes)!=-1) {fos.write (tmpbytes,0,length);}} After the file, get rid of the header file. AMR formatted file header information is 6 bytes else{while (fis.read (tmpbytes)!=-1) {fos.write (tmpbytes,6,length-6);}} Fos.flush (); Fis.close ();} Fos.close ();//The end of all file merges, close the output stream log.i ("info", "This recording file:" +realfile.getname () + "saved to:" +realfile.getabsolutepath () + "directory ");} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} Delete the merged temporary file for (file file:tempfiles) {if (file.exists ()) {File.delete ();}}}

Way Two:by FileChannel Way

/** * by filechannel mode * */public void MergeFiles2 () {File realfile=getfile (false); FileChannel mfilechannel;try {fileoutputstream fos=new fileoutputstream (realfile); Mfilechannel=fos.getchannel ();    FileChannel Infilechannel;    for (file file:tempfiles) {                     infilechannel=new fileinputstream (file). Getchannel ();        The following should be based on different files minus the corresponding file header (here does not cut the file header, the actual application should be subtracted)        Infilechannel.transferto (0, Infilechannel.size (), mfilechannel);                     Infilechannel.close ();    }           Fos.close ();    Mfilechannel.close ();} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}

Way Three:by Randomaccessfile Way

/** * by randomaccessfile mode * */public void MergeFiles3 () {try{    File realfile=getfile (false);    FileOutputStream fos = new FileOutputStream (realfile);    Randomaccessfile RA = null;    for (int i = 0; i < tempfiles.size (); i++) {        RA = new Randomaccessfile (Tempfiles.get (i), "R");    if (i! = 0) {    ra.seek (6);//Skip file header of AMR file    }    byte[] buffer = new byte[1024 * 8];    int len = 0;    while (len = ra.read (buffer))! =-1) {    fos.write (buffer, 0, Len);    }   }   Ra.close ();   Fos.close ();  } catch (Exception e) {  e.printstacktrace ();  }}


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.