我必须从存储在设备内存中的文本文件中获取大量数据。并且需要将从文件读取的数据块发送到服务器。由于文件中包含大量数据,因此我想在从文件系统中获取数据时将其分成多个块。目前,我的逻辑是一次性获取数据。
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) {}
这有效,但是我想将数据块大小设置为固定值。然后应迭代获取整个文件数据并发送到服务器。您能建议我一种方法吗?
最佳答案
我使用了以下实用程序方法:
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;
}