如何获取给定范围内的文件字节?

时间:2016-06-08 12:34:49

标签: java file bytearray

如何获取给定范围内的文件字节?文件可能非常大,因此将所有字节保留在内存中并不是一个好主意。我可以逐字节读取文件吗?读得那么正常吗?

3 个答案:

答案 0 :(得分:1)

我同意@Berger,你可以在java中使用 RandomAccessFile 。您可以使用类似下面的代码来随机读取文件。

RandomAccessFile f = new RandomAccessFile("FilePath","r");
byte[] buffer = new byte[1024];
f.read(buffer, 10, 100);

以下是java-doc中的read()方法文档 -

/**
 * Reads up to <code>len</code> bytes of data from this file into an
 * array of bytes. This method blocks until at least one byte of input
 * is available.
 * <p>
 * Although <code>RandomAccessFile</code> is not a subclass of
 * <code>InputStream</code>, this method behaves in exactly the
 * same way as the {@link InputStream#read(byte[], int, int)} method of
 * <code>InputStream</code>.
 *
 * @param      b     the buffer into which the data is read.
 * @param      off   the start offset in array <code>b</code>
 *                   at which the data is written.
 * @param      len   the maximum number of bytes read.
 * @return     the total number of bytes read into the buffer, or
 *             <code>-1</code> if there is no more data because the end of
 *             the file has been reached.
 * @exception  IOException If the first byte cannot be read for any reason
 * other than end of file, or if the random access file has been closed, or if
 * some other I/O error occurs.
 * @exception  NullPointerException If <code>b</code> is <code>null</code>.
 * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
 * <code>len</code> is negative, or <code>len</code> is greater than
 * <code>b.length - off</code>
 */
public int read(byte b[], int off, int len) throws IOException {
    return readBytes(b, off, len);
}

答案 1 :(得分:0)

一般来说,将整个文件加载到内存中并不是一个好主意,除非你知道它总是足够小以适应你的内存并且你不会并行加载多个文件,否则你可能会遇到{{1 }}。

如果你想读取一个文件,你确实可以通过方法read()逐字节读取,但实际上它用于非常特殊的用例,你需要读取几个字节,因为它不是优化的方式来读取整个文件。

这种情况下的通用代码是:

OOME

如果你想更快地读取文件,你应该使用方法read(byte[] b),它允许重用先前由调用者代码创建的字节数组,并像你想要的那样读取一系列字节。 / p>

这种情况下的通用代码是:

int data;
while ((data = input.read()) != -1) {
    // Do something with data
}

如果你想在开始读取你的字节范围之前跳过一些字节,你确实可以使用int length; byte[] data = new byte[someSizeHere]; while ((length = input.read(data)) != -1) { // Do something with the bytes in data between index 0 and length - 1 } 及其方法seek(long)

答案 2 :(得分:0)

使用RandomAccessFile打开文件,寻找起始偏移量,定义缓冲区长度并完全读取缓冲区。 try-with-resources语句负责关闭RandomAccessFile

public static byte[] readByteRange(String sourceFilePath, long startingOffset, int length) throws IOException
{
    try (RandomAccessFile randomAccessFile = new RandomAccessFile(sourceFilePath, "r"))
    {
        byte[] buffer = new byte[length];
        randomAccessFile.seek(startingOffset);
        randomAccessFile.readFully(buffer);

        return buffer;
    }
}