作者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
1F:推 popcorny:IOUtils.copyLarge(in, out, new byte[4*1024*1024]) 06/04 22:24
2F:→ popcorny:说真的 用它预设的就好了 那个buffersize影响很小 06/04 22:25
3F:→ mib30213:如果是大於10M档案,要怎麽定位out的位址 06/04 22:27
4F:推 popcorny:buffer固定就好啦..1g的file用4m的buffer也够了 06/04 22:31
5F:→ popcorny:使用方法就是开out = FileOuptStream(yourFile)就可以用 06/04 22:31