作者mib30213 (夏天來了)
看板AndroidDev
標題Re: [問題] 讀檔到buffer
時間Tue Jun 4 22:20:03 2013
※ 引述《popcorny (畢業了..@@")》之銘言:
: ※ 引述《mib30213 (夏天來了)》之銘言:
: : 我想要讀入一個未知大小的檔案,
: : 並讀入buffer ,分成4MB 為一個byte array
: : 該怎麼寫會比較好!?卡關卡很久
: : FileInputStream fileInputStream = null;
: : FileOutputStream fileOutputStream = null;
: : byte[] buffer = new byte[available()];
: : while((length = fileInputStream.read(buffer)) != -1) {
: : // 將陣列資料寫入目的檔案
: : fileOutputStream.write(buffer, 0, length);
: : }
: while((length = fileInputStream.read(buffer)) > 0) {
: fileOutputStream.write(buffer, 0, length);
: }
: fileInputStream.close();
: fileOutputStream.close();
: 像這種需求我都用apache commons-io裡面的
: IOUtils.copy(in, out);
: 簡單又正確
這樣是把整個讀到的檔案寫到buffer,如果是分成4M呢?
另外IOUtils.copy要怎麼指定大小呢?
謝謝你的解答^^
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 1.169.169.30
推 popcorny:IOUtils.copyLarge(in, out, new byte[4*1024*1024]) 06/04 22:24
→ popcorny:說真的 用它預設的就好了 那個buffersize影響很小 06/04 22:25
→ mib30213:如果是大於10M檔案,要怎麼定位out的位址 06/04 22:27
推 popcorny:buffer固定就好啦..1g的file用4m的buffer也夠了 06/04 22:31
→ popcorny:使用方法就是開out = FileOuptStream(yourFile)就可以用 06/04 22:31