【发布时间】:2013-09-21 00:20:21
【问题描述】:
我正在尝试一次处理 10MB 字节数组的大文件。 我正在尝试一次获取一个字节数组(不是为大文件获取整个字节数组并拆分字节数组,毕竟问题是由于内存造成的)
这是我目前所拥有的:
private byte[] readFile(File file, int offset) throws IOException
{
BufferedInputStream inStream = null;
ByteArrayOutputStream outStream = null;
byte[] buf = new byte[1048576];
int read = 0;
try
{
inStream = new BufferedInputStream(new FileInputStream(file));
outStream = new ByteArrayOutputStream();
long skipped = inStream.skip(offset);
read = inStream.read(buf);
if (read != -1)
{
outStream.write(buf, 0, read);
return outStream.toByteArray();
}
}
finally
{
if (inStream != null) {try {inStream.close();} catch (IOException e) {}}
if (outStream != null) {try {outStream.close();} catch (IOException e) {}}
}
return null;
参数offset 也将以 10MB 为增量。
所以我遇到的问题是,即使 skipped long 变量让我跳过了 1048576 个字节,我想通过调用 readFile(file, 1048576) 接收到的第二个 10MB 与来自的第一个字节数组相同前 10MB。因此它并没有真正跳过前 10MB。
这里有什么问题?有没有其他方法可以实现这个想法?
【问题讨论】:
-
您不会以相同的数量增加偏移量,因此下一个偏移量将是 2x 1048576。因此您始终获得相同的字节。
-
我正确地偏移了它,2x 1048576 将是第三个块,因为第一个块的偏移量为 0 对吗?
-
所以您一遍又一遍地阅读
File并从方法外部增加offset变量?你能添加调用你的方法的代码吗?
标签: java android file stream bytearray