比较好的建立站点,贵阳网站优化排名,小游戏网址代码,美词网站建设以下介绍合并文件的几种方式#xff0c;并通过合并amr文件来举例介绍合并文件的详细流程。amr格式的文件头是6字节#xff0c;所以在进行文件合并的时候要减去除第一个文件以外的其它文件的文件头。 注意#xff1a;不同文件的文件头是不一样的#xff0c;所以在合并的时候… 以下介绍合并文件的几种方式并通过合并amr文件来举例介绍合并文件的详细流程。amr格式的文件头是6字节所以在进行文件合并的时候要减去除第一个文件以外的其它文件的文件头。 注意不同文件的文件头是不一样的所以在合并的时候依据不同文件对应的减去合并文件的文件头。 步骤一获取要合并的文件及创建合并后保存的文件 /**用于存放要合并的文件的集合**/
ListFiletempFilesnew ArrayListFile();
/**合并之后的文件**/
File finalFile; /*** 创建用于合并之后的文件* param isTempFile 是否为暂时文件* return soundFile File* */private File getFile(boolean isTempFile) {// TODO Auto-generated method stub finalFilenull;if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {Log.w(Waring, 检測到你的手机没有插入SD卡请插入SD后再试);} //获取系统的24小时制时间作为文件名称(HH为24小时制hh为12小时制)SimpleDateFormat simpleDateFormatnew SimpleDateFormat(yyyy-MM-dd-HH-mm-ss,Locale.getDefault()); String fileNamesimpleDateFormat.format(new Date()).amr; if (isTempFile) {//假设是暂时文件fileNametempfileName;}try {File parentFile new File(Environment.getExternalStorageDirectory().getCanonicalFile()/Recorder);if (!parentFile.exists()||parentFilenull) {//假设文件夹不存在parentFile.mkdirs();//创建parentFile文件夹}finalFilenew File(parentFile, fileName);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} return finalFile; } 步骤二合并文件 方式一 通过FileOutputStream、与FileInputStream方式 /*** 通过FileOutputStream、与FileInputStream方式* 将多个文件进行合并并删除原文件* */public void mergeFiles1() {// TODO Auto-generated method stubif (tempFiles.isEmpty()) return;//假设还没录制则不进行合并File realFilegetFile(false);try {FileOutputStream fosnew FileOutputStream(realFile); for (int i 0; i tempFiles.size(); i) {//遍历tempFiles集合合并全部暂时文件 FileInputStream fisnew FileInputStream(tempFiles.get(i));byte[] tmpBytes new byte[fis.available()];int length tmpBytes.length;//文件长度//头文件if(i0){while(fis.read(tmpBytes)!-1){fos.write(tmpBytes,0,length);}} //之后的文件去掉头文件就能够了.amr格式的文件的头信息为 6字节else{while(fis.read(tmpBytes)!-1){ fos.write(tmpBytes,6,length-6);}} fos.flush();fis.close(); }fos.close();//全部的文件合并结束关闭输出流Log.i(info, 此次录音文件realFile.getName() 已保存到realFile.getAbsolutePath()文件夹下);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}//删除合并过的暂时文件for (File file:tempFiles) {if (file.exists()) {file.delete();}}}方式二 通过FileChannel方式 /*** 通过FileChannel方式* */public void mergeFiles2() {File realFilegetFile(false);FileChannel mFileChannel;try {FileOutputStream fosnew FileOutputStream(realFile); mFileChannelfos.getChannel(); FileChannel inFileChannel;for(File file:tempFiles){ inFileChannelnew FileInputStream(file).getChannel();//以下应该依据不同文件减去对应的文件头这里没有剪去文件头实际应用中应当减去inFileChannel.transferTo(0, inFileChannel.size(), mFileChannel); inFileChannel.close();} fos.close();mFileChannel.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}方式三通过RandomAccessFile方式 /*** 通过RandomAccessFile方式* */public void mergeFiles3() {try{ File realFilegetFile(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);//跳过amr文件的文件头}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();} }转载于:https://www.cnblogs.com/mengfanrong/p/4040069.html