Re: [問題] 讀檔到buffer

看板AndroidDev作者 (夏天來了)時間12年前 (2013/06/04 22:20), 編輯推噓2(203)
留言5則, 2人參與, 最新討論串3/4 (看更多)
※ 引述《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

06/04 22:24, , 1F
IOUtils.copyLarge(in, out, new byte[4*1024*1024])
06/04 22:24, 1F

06/04 22:25, , 2F
說真的 用它預設的就好了 那個buffersize影響很小
06/04 22:25, 2F

06/04 22:27, , 3F
如果是大於10M檔案,要怎麼定位out的位址
06/04 22:27, 3F

06/04 22:31, , 4F
buffer固定就好啦..1g的file用4m的buffer也夠了
06/04 22:31, 4F

06/04 22:31, , 5F
使用方法就是開out = FileOuptStream(yourFile)就可以用
06/04 22:31, 5F
文章代碼(AID): #1HhVUL5t (AndroidDev)
討論串 (同標題文章)
文章代碼(AID): #1HhVUL5t (AndroidDev)