Several ways to merge files with Android __java several ways to merge files

Source: Internet
Author: User
Tags stub

Here are several ways to merge files, and use the AMR file to illustrate the specific process of merging files. The AMR file header is 6 bytes, so you subtract file headers from files other than the first file when merging files.

Note: The file headers for different files are not the same, so the file headers for the merged files are subtracted from each file at the time of merging.

Step one: Get the files you want to merge and create the files that you saved after merging

/** is used to hold the collection of files to be merged **/
list<file>tempfiles=new arraylist<file> ();
/** the merged files **/file
finalfile;
	/** * Create a file for merging * @param istempfile is temporary file * @return soundfile file * * * * Private file GetFile (Boolean Istempf
		ile) {//TODO auto-generated method stub finalfile=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.")
		"); //Get System 24-hour time as filename (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"); if (!parentfile.exists () | |
		Parentfile==null) {//If directory does not exist Parentfile.mkdirs ();//Create parentfile directory} finalfile=new File (Parentfile, fileName);
		catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();	
	return finalfile; }

Step two: Merging files

Way one: Through FileOutputStream, with FileInputStream way

	/** * Merge multiple files through FileOutputStream, FileInputStream, and delete the original file * */public void mergeFiles1 () {//TODO Auto-gen
		erated method Stub if (Tempfiles.isempty ()) return;//does not merge File Realfile=getfile (false) if it is not yet recorded;		
			try {fileoutputstream fos=new fileoutputstream (realfile); for (int i = 0; i < tempfiles.size (); i++) {//Traverse tempfiles Collection, merge all temporary files FileInputStream fis=new fileinputstream (te
				Mpfiles.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, remove the header file. The header information for a file in AMR format is 6 bytes else{while (Fis.read (tmpbytes)!=-1) {Fos.write (t
					MPBYTES,6,LENGTH-6);
				} fos.flush ();		
			Fis.close (); Fos.close ()///All files merged to close, turn off output stream log.i ("info", "This recording file:" +realfile.getname () + "saved to:" + Realfile.getabsolutepath ()
		+ "directory"); catch (Exception e) {//TODO Auto-generatEd Catch Block E.printstacktrace ();
			//Deletes the merged temporary file for (file file:tempfiles) {if (file.exists ()) {file.delete (); }
		}
	}

Mode two: through the FileChannel way

	/**
	 * */public
	void MergeFiles2 () {
		File Realfile=getfile (False)
		via filechannel mode * * * 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 subtracted from the corresponding file header (there is no clipping of the file head, 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 block
			e.printstacktrace ();
		}
	}

Mode three: through the Randomaccessfile way

	/**
	 * */public
	void MergeFiles3 () {
		try{				
		    File realfile=getfile (False)
		    via randomaccessfile mode * * * 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 the 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 ();
		  }		
	}


Related Article

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.