Android開發之合併檔案的幾種方式

2021-09-06 18:51:47 字數 2043 閱讀 7501

以下介紹合併檔案的幾種方式,並通過合併amr檔案來舉例介紹合併檔案的詳細流程。

amr格式的檔案頭是6位元組,所以在進行檔案合併的時候要減去除第乙個檔案以外的其它檔案的檔案頭。

注意:不同檔案的檔案頭是不一樣的,所以在合併的時候依據不同檔案對應的減去合併檔案的檔案頭。

步驟一:獲取要合併的檔案及建立合併後儲存的檔案

/**用於存放要合併的檔案的集合**/

listtempfiles=new arraylist();

/**合併之後的檔案**/

file finalfile;

/**

* 建立用於合併之後的檔案

* @param istempfile 是否為暫時檔案

* @return soundfile file

* */

private file getfile(boolean istempfile)

//獲取系統的24小時制時間作為檔名稱(hh為24小時制,hh為12小時制)

******dateformat ******dateformat=new ******dateformat(

"yyyy-mm-dd-hh-mm-ss",locale.getdefault());

string filename=******dateformat.format(new date())+".amr";

if (istempfile)

try

finalfile=new file(parentfile, filename);

} catch (ioexception e)

return finalfile;

}

步驟二:合併檔案

方式一: 通過fileoutputstream、與fileinputstream方式

/**

* 通過fileoutputstream、與fileinputstream方式

* 將多個檔案進行合併,並刪除原檔案

* */

public void mergefiles1()

}

//之後的檔案,去掉頭檔案就能夠了.amr格式的檔案的頭資訊為 6位元組

else

}

fos.flush();

fis.close();

}fos.close();//全部的檔案合併結束,關閉輸出流

log.i("info", "此次錄音檔案:"+realfile.getname()+" 已儲存到:"+

realfile.getabsolutepath()+"資料夾下");

} catch (exception e)

//刪除合併過的暫時檔案

for (file file:tempfiles)

} }

方式二:

通過filechannel方式

/**

* 通過filechannel方式

* */

public void mergefiles2()

fos.close();

mfilechannel.close();

} catch (exception e)

}

方式三:

通過randomaccessfile方式

/**

* 通過randomaccessfile方式

* */

public void mergefiles3()

byte buffer = new byte[1024 * 8];

int len = 0;

while ((len = ra.read(buffer)) != -1)

}ra.close();

fos.close();

} catch (exception e)

}

Android開發之SD卡上檔案操作

1.得到儲存裝置的目錄 sdcard 一般情況下 sdpath environment.getexternalstoragedirectory 2.判斷sd卡上的資料夾是否存在 通過file物件的exists 方法。判斷檔案是否已經存在 public boolean checkfileexists ...

Android開發之SD卡上檔案操作

1.得到儲存裝置的目錄 sdcard 一般情況下 sdpath environment.getexternalstoragedirectory 2.判斷sd卡上的資料夾是否存在 通過file物件的exists 方法。判斷檔案是否已經存在 public boolean checkfileexists ...

Android開發之簡訊

1.sms主要結構 id 簡訊序號,如100 thread id 對話的序號,如100,與同乙個手機號互發的簡訊,其序號是相同的 person 發件人,如果發件人在通訊錄中則為具體姓名,陌生人為null date 日期,long型,如1346988516,可以對日期顯示格式進行設定 protocol...