我必须从存储在设备内存中的文本文件中获取大量数据。并且需要将从文件读取的数据块发送到服务器。由于文件具有巨大的数据,因此我希望在从文件系统获取数据时将数据分成块。目前我的逻辑是一次抓取数据。
try {
fc = (FileConnection) Connector.open(path, Connector.READ);
if (fc.exists()) {
int size = (int) fc.fileSize();
is = fc.openInputStream();
byte bytes[] = new byte[size];
is.read(bytes, 0, size);
//System.out.println("Text: " + str);
}
} catch (Exception ioe) {}这是可行的,但我希望将数据块大小设置为固定值。然后迭代地获取整个文件数据并发送到服务器。你能给我推荐一种方法吗?
发布于 2012-08-29 01:04:31
我使用了以下实用程序方法:
public static int copy (InputStream is, OutputStream out) throws IOException {
byte [] buff = new byte[1024];
int len = is.read(buff);
int total = 0;
while (len > 0) {
total += len;
out.write(buff, 0, len);
len = is.read(buff);
}
return total;
}发布于 2012-08-28 21:34:28
您可以使用InputStream.skip跳转到文件的特定部分。
在某个地方建立一个索引,以了解你已经阅读了多少。
使用固定大小的块(几个Kos),并在读取时跳转到块大小*索引。
https://stackoverflow.com/questions/12160361
复制相似问题