0

我有一个 45MB 的大文件,假设我可用的内存有限,我想先读取 5MB,依此类推。

我需要使用 Java 来做到这一点。有人请帮帮我。

提前致谢!!

4

2 回答 2

1

I think you can just use basic byte streams for this. Check out http://docs.oracle.com/javase/tutorial/essential/io/bytestreams.html

I'd use the read(byte[] b) method of a FileInputStream class which 'Reads up to b.length bytes of data from this input stream into an array of bytes'

read(byte[] b, int off, int len) method would also allow you to do this with an offset for previously read data.

于 2012-09-30T18:39:06.077 回答
0

以下代码将从文件中读取 5000 字节 (5MB)。

byte[] bytes = new byte[5000]; 
    DataInputStream dis = new DataInputStream(new FileInputStream(file)); 
      int read = 0;
      int numRead = 0;
      while (read < bytes.length && (numRead=dis.read(bytes, read, bytes.length-read)) >= 0) {
        read = read + numRead;
      }
于 2012-09-30T18:35:12.610 回答