读取文件中的Java / Android的一个片段 [英] Read a segment of a file in Java / Android

查看:114
本文介绍了读取文件中的Java / Android的一个片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我敢肯定,这可能是一个简单的问题,但不幸的是,这是我第一次使用Java和工作在Android SDK。

I'm sure this might be a simple question, but unfortunately this is my first time using Java and working the Android SDK.

我在Android上使用Apache HTTP库使用MultipartEntity上传文件,尤其如此。

I am uploading files on Android using the Apache HTTP libraries, in particular using the MultipartEntity.

我要上传,让我给他们发送的文件块,一旦完成,他们会重新组装块的服务。我想利用这个功能的优势。

I'm uploading to a service that allows me to send them chunks of the file, and once complete, they'll reassemble the chunks. I'd like to take advantage of this feature.

下面的情景。

文件FOO.BAR为20 MB。我想它拆分成一些任意的块大小,比方说1 MB,这意味着20块。大块#3,#14失败(也许是蜂窝/ WiFi连接是坏的)。我现在可以重新上传只是这两大块,一切都会好的。

File FOO.BAR is 20 MB. I'd split it into some arbitrary chunk size, let's say 1 MB, which means 20 chunks. Chunks #3 and #14 fail (maybe the cellular/WiFi connection was bad). I can now re-upload just these two chunks and everything will be good.

我想知道的是我如何读取文件的一部分(如3MB和4MB之间的数据)?

What I'd like to know is how can I read only part of a file (like the data between 3MB and 4MB)?

文件块应该是一个InputStream或File对象。

The file piece should be an InputStream or File object.

谢谢, 诚

推荐答案

您既可以使用的跳过(长)方法跳过InputStream的字节数,或者你可以在File对象创建的RandomAccessFile并调用它的<一个href="http://download-llnw.oracle.com/javase/6/docs/api/java/io/RandomAccessFile.html#seek%28long%29"相对=nofollow>求(长)方法将指针设置到该位置,所以你可以从那里开始阅读。

You can either use the skip(long) method to skip the number of bytes in the InputStream or you can create a RandomAccessFile on the File object and call its seek(long) method to set the pointer to that position so you can start reading from there.

快速测试的4MB +文件(3m和4MB之间),低于读取,读取的数据写入一个文件。

The quick test below reads in a 4mb+ file (between 3m and 4mb) and writes the read data to an ".out" file.

import java.io.*;
import java.util.*;

public class Test {

    public static void main(String[] args) throws Throwable {
       long threeMb = 1024 * 1024 * 3;
       File assembled =  new File(args[0]); // your downloaded and assembled file
       RandomAccessFile raf = new RandomAccessFile(assembled, "r"); // read
       raf.seek(threeMb); // set the file pointer to 3mb
       int bytesRead = 0;
       int totalRead = 0;
       int bytesToRead = 1024 * 1024; // 1MB (between 3M and 4M

       File f = new File(args[0] + ".out");
       FileOutputStream out = new FileOutputStream(f);

       byte[] buffer = new byte[1024 * 128]; // 128k buffer 
       while(totalRead < bytesToRead) { // go on reading while total bytes read is
                                        // less than 1mb
         bytesRead = raf.read(buffer);
         totalRead += bytesRead;
         out.write(buffer, 0, bytesRead);
         System.out.println((totalRead / 1024));
       }
    }
}

这篇关于读取文件中的Java / Android的一个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆