Re: [問題] 讀檔到buffer
※ 引述《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
06/04 22:24, 1F
→
06/04 22:25, , 2F
06/04 22:25, 2F
→
06/04 22:27, , 3F
06/04 22:27, 3F
推
06/04 22:31, , 4F
06/04 22:31, 4F
→
06/04 22:31, , 5F
06/04 22:31, 5F
討論串 (同標題文章)