我有一個16mb的二進制文件,我想讀取字節而不使用任何循環。就像如果我想讀16個字節,我可以傳遞16個方法(如果有的話),它給我我想要的結果...現在我正在使用循環讀取字節,但我的應用程序是如此巨大,我害怕它不會變得遲緩。給我一些提示。讀取字節而不使用任何循環
非常感謝。
我有一個16mb的二進制文件,我想讀取字節而不使用任何循環。就像如果我想讀16個字節,我可以傳遞16個方法(如果有的話),它給我我想要的結果...現在我正在使用循環讀取字節,但我的應用程序是如此巨大,我害怕它不會變得遲緩。給我一些提示。讀取字節而不使用任何循環
非常感謝。
import java.io.*;
public class NoLoopReader {
public static void main (String[] args) throws Exception {
String fileName = args[0];
InputStream is = new BufferedInputStream(new FileInputStream(fileName));
File file = new File(fileName);
int size = (int)file.length();
byte[] buffer = new byte[size];
is.read(buffer,0,size);
}
}
'is.read(byte [],int,int)'在實現中包含for循環。包裝到BufferedInputStream中的 – 2010-08-13 11:00:04
是一個糟糕的主意,無助於此。 – bestsss 2011-01-20 16:50:36
我可以給你兩個答案,但他們不實際。在現實生活中,您將使用循環來讀取字節。
有效答案1
public byte[] readWithNoLoop(InputStream in, int size) {
byte[] result = new byte[16777216]; // 16 MByte
byte b = 0;
if ((b = in.read()) >= 0) result[0] = b;
if ((b = in.read()) >= 0) result[1] = b;
if ((b = in.read()) >= 0) result[2] = b;
// ...
if ((b = in.read()) >= 0) result[16777215] = b;
return b;
}
有效答案2
使用可以並行地讀取該文件一個大規模並行系統。您需要16777216個處理單元和一個支持文件存儲系統,但您可以在O(1)(理論上)中閱讀。
如果遇到大規模的性能比較問題,同時讀取文件時,請檢查您是否使用的BufferedInputStream,從一個「正常」流讀取字節殺死性能。 (Example)
如果它仍然沒有幫助,那麼看看java.nio
類。他們可以將文件映射到內存。 Grep example應該給你一個方向。
是的,但是:1)包含循環的實現; 2)您需要循環訪問您的客戶端代碼以安全地讀取它。我並不是說這是一個壞主意,我指出這些要求是荒謬的,更好的答案是「WTF是否想爲此做」,而不是試圖遵守它們。 – 2010-08-13 11:13:17
是的,我認爲需求是關於在一次調用中讀取n個字節,而不是無效地逐一讀取它們。 – hhbarriuso 2010-08-13 18:25:27
您可能會在磁盤檢查文件大小,創建適當的緩衝,並使用這種讀取方式做批量加載(你可以看到它調用本地的ReadBytes)。當然,我不知道是否有的ReadBytes的內部沒有迴路(最有可能有,但可能取決於實現JVM的)... :)
在的FileInputStream
/**
* Reads up to <code>b.length</code> bytes of data from this input
* stream into an array of bytes. This method blocks until some input
* is available.
*
* @param b the buffer into which the data is 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 an I/O error occurs.
*/
public int read(byte b[]) throws IOException {
回報的ReadBytes( b,0,b.length); }
/**
* Reads up to <code>len</code> bytes of data from this input stream
* into an array of bytes. If <code>len</code> is not zero, the method
* blocks until some input is available; otherwise, no
* bytes are read and <code>0</code> is returned.
*
* @param b the buffer into which the data is read.
* @param off the start offset in the destination array <code>b</code>
* @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 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>
* @exception IOException if an I/O error occurs.
*/
public int read(byte b[], int off, int len) throws IOException {
return readBytes(b,off,len); }
你認爲在你傳遞數字的神奇方法中發生了什麼? – 2010-08-13 10:35:07
hahahaha明顯循環:d – sajjoo 2010-08-13 10:35:44
但我的團隊領導迫使我搜索一些我在方法中問他的東西,他必須有一個循環,但他說不,它可能不同。 – sajjoo 2010-08-13 10:36:46