【问题标题】:Downloading data in chunks in J2ME在 J2ME 中分块下载数据
【发布时间】:2012-08-23 00:37:29
【问题描述】:

我必须从存储在设备内存中的文本文件中获取大量数据。并且需要将从文件中读取的数据块发送到服务器。由于文件有大量数据,我想在从文件系统获取数据时将数据分成块。目前我的逻辑是一次性获取数据。

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) {}

这可行,但我想将数据块大小设置为固定值。然后迭代地它应该获取整个文件数据并发送到服务器。你能建议我一种方法吗?

【问题讨论】:

    标签: java multithreading java-me inputstream


    【解决方案1】:

    我使用了以下实用方法:

    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;
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用 InputStream.skip 跳转到文件的特定部分。

      在某处保留索引以了解您已经阅读了多少。

      使用固定大小的chunk(几个kos),阅读时跳转到chunk-size*index。

      【讨论】:

      • 我是否必须将从存储卡中获取文件和打开流连接的整个逻辑放在一个循环中?
      • 我不这么认为,但你可以。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多