Java 在J2ME中分块下载数据
我必须从存储在设备内存中的文本文件中获取大量数据。并且需要将从文件中读取的数据块发送到服务器。由于文件中有大量数据,我想在从文件系统获取数据时将数据分成若干块。目前我的逻辑是一次性获取数据Java 在J2ME中分块下载数据,java,multithreading,java-me,inputstream,Java,Multithreading,Java Me,Inputstream,我必须从存储在设备内存中的文本文件中获取大量数据。并且需要将从文件中读取的数据块发送到服务器。由于文件中有大量数据,我想在从文件系统获取数据时将数据分成若干块。目前我的逻辑是一次性获取数据 try { fc = (FileConnection) Connector.open(path, Connector.READ); if (fc.exists()) { int size = (int) fc.fileSize(
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) {}
这是可行的,但我想将数据块大小设置为固定值。然后迭代地获取整个文件数据并发送到服务器。你能给我提个建议吗 您可以使用InputStream.skip跳转到文件的特定部分 把索引放在某个地方,知道你已经读了多少
使用固定大小的块(几个KO),阅读时跳转到块大小*索引。我使用了以下实用方法:
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;
}
我必须把从存储卡获取文件和打开流连接的整个逻辑放在一个循环中吗?我不这么认为,但你可以。