0
所以我已經實現了使用二進制搜索方法,其搜索文件的工作程序中的二進制搜索:緩衝
public int BSearch(int x1, int x2) throws IOException {
int current_key;
middle=(x1+x2)/2;
if(x1>x2) {
middle=-1; //middle==-1 is condition of 'key not found'
return middle;
}
MyFile.seek(middle*4);
current_key=MyFile.readInt();
da++;
if(current_key==key) {
return middle;
}
else if(key<current_key) {
x2=middle-1;
return BSearch(x1,x2);
}
else {
x1=middle+1;
return BSearch(x1,x2);
}
}
現在我想,所以它讀取文件片逐片(比如其改造每次1KB)放入緩衝區,然後二進制搜索該緩衝區。如果在該緩衝區中找不到密鑰,我會繼續讀取該文件等等。我想澄清,雖然該緩衝區是這樣的手工緩衝(指正):
byte[] buf = new byte[1024];
MyFile.read(buf);
ByteArrayInputStream bis= new ByteArrayInputStream(buf1);
DataInputStream ois= new DataInputStream(bis);
current_key=ois.readInt();
的一個大問題(其中包括)是我不知道我怎麼會從的某個位置讀取緩衝區
爲什麼你不能只使用整個文件? – 2013-03-16 14:26:02
這個想法是,我想使用小緩衝區,所以我可以用同樣的方式處理大文件(如5 GB)。當然,我不能將5GB讀入主內存。 – xouris 2013-03-16 14:38:18
我不認爲整個文件需要加載到內存。你可以簡單地使用'seek'並讀取相關的字節。 'seek'函數接受一個'long',它可以處理高達8 EB字節的數據,這應該足夠了。 – 2013-03-16 15:08:54