使用Java从文件读取字节数组 [英] Read a byte array from file with Java
问题描述
大家好,
我在Java中有一个作业,其中一部分是从文件中读取字节数组,从指定的偏移量开始并具有给定的长度.
我的解决方案是使用类
Hi everybody,
I have an assignment in Java, one of its part is read a byte array from file, start at a specified offset and have a given length.
My solution is using method
read()
的方法
FileInputStream
首先,我将所有文件读取为数组.然后,将指定长度的指定偏移量的子数组复制到新数组,然后返回新数组.
但是,由于Java应用程序的内存很小,因此我的解决方案不适用于200 MB的文件.
如何在不读取整个文件的情况下读取我想要的文件的一部分?
感谢您阅读我的问题.
First, I read all file as an array. Then I copy the sub-array, from specified offset with given length to new array and return the new array.
But, because the memory for a Java application is small, my solution does not work with a 200 MB file.
How c an I read a part of file that I want without reading the whole file?
Thanks for reading my question.
推荐答案
请参阅: ^ ],该类为java.io.RandomAccessFile
.
当理查德(Richard)对您熟悉随机访问的概念(或只是术语)表示关注时,您可以看到以下文章:
http://en.wikipedia.org/wiki/Random_access [ http://en.wikipedia.org/wiki/Random-access_memory [
See: http://download.oracle.com/javase/1.4.2/docs/api/java/io/RandomAccessFile.html#readFully%28byte[],%20int,%20int%29[^], the class isjava.io.RandomAccessFile
.
As Richard expressed concern about your familiarity with the concept (or just the terminology) of random access, you can see these articles:
http://en.wikipedia.org/wiki/Random_access[^],
http://en.wikipedia.org/wiki/Random-access_memory[^].
Files also allow random access to any record. Even in the case of disk or even tape memory it''s always possible to relocate the reading head to any position. For such devices, this can be very expensive. So, using buffered stream will highly facilitate random-access operations.
—SA
这篇关于使用Java从文件读取字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!