2016-06-08 76 views
1

如何獲得給定範圍內的文件字節?文件可能非常大,因此將所有字節保存在內存中不是個好主意。我可以逐字節讀取文件嗎?閱讀是否正常?如何獲得給定範圍內的文件字節?

+2

查看'RandomAccessFile'。 – Berger

+0

@NicolasFilotto,二進制 – Tony

回答

1

我同意@Berger,你可以在java中使用RandomAccessFile。你可以使用下面的代碼來隨機讀取文件。

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

下面是read()方法從Java的doc-文檔

/** 
* 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); 
} 
+0

這看起來不對:該文檔指出,您可能無法獲得完全所需的字節數。所以......你應該讓你的代碼循環,直到這種情況。 – GPI

+0

@GPI,你能提供一些代碼嗎? – Tony

+0

您可以查看DataInputStream#readFully的源代碼,它可以做到這一點。 – GPI

0

一般來說是很少到整個文件加載到內存中是一個好主意,除非你知道它總會足夠小以適應您的記憶,並且您不會同時加載多個文件,否則您可能會面臨OOME

如果您想要讀取文件,您確實可以通過方法read()來逐字節讀取,但實際上它用於需要讀取少量字節的非常特定的用例,因爲它不是一種優化方式閱讀整個文件。

在這種情況下公共代碼是:

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

如果你想閱讀您的文件更快,你應該使用的方法read(byte[] b),它允許重用以前由主叫用戶代碼中創建一個字節數組和閱讀像你想要做的一樣的字節範圍。

在這種情況下公共代碼是:

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 
} 

如果你想在開始讀你的字節範圍之前要跳過一些字節,你的確可以使用RandomAccessFile及其方法seek(long)

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; 
    } 
} 
相關問題