java - RandomAccessFile readInt -
how read numbers file???
when use readint method big number, , not equals number file.
how fix it???
scanner not idea, becouse file contains more 1000 millions numbers... takes long time...
yes, text file.
file contains numbers devided space symbols. example ( test.txt )
1 2 4 -4004 15458 8876
public static void readbymemorymappedfile(int buffer[], string filename) throws ioexception { int count = 0; randomaccessfile raf = new randomaccessfile(filename, "r"); try { mappedbytebuffer mapfile = raf.getchannel().map(mapmode.read_only, 0, raf.length()); stringbuilder b = new stringbuilder(); try { while (mapfile.hasremaining()) { byte read = mapfile.get(); if (read == ' ' && b.length() > 0) { buffer[count++] = mapfile.getint();//integer.parseint(b.tostring()); b.delete(0, b.length()); } else { b.append((char) read); } } } catch (bufferunderflowexception e) { // Всё, файл закончился } if (b.length() > 0) { buffer[count++] = integer.parseint(b.tostring()); } } { raf.close(); } } so, attached report:
// operation: time reading: 39719 // t0 reading: 28297 // t1 reading: 56719 // t2 reading: 125735 // t3 reading: 199000 // t4 t0 < t1 < t2 < t3 < t4
how change behavior of program this: t0 ~ t1 ~ t2 ~ t3 ~ t4 ???
if want randomly access data, need able determine start , finish. text format can tricky , may have read previous lines/text find 1 want.
with binary formats, may able calculate want read, need know how number encoded. e.g. big endian or little endian?
scanner may not optimal text, , useless binary data, if may more fast enough.
much of time taken scan large file time takes read off disk (assuming won't fit in memory) can speed if file compresses well, e.g. text full of numbers does. instead of taking 20 seconds read might take 2 seconds if compressed. (and might fit in os file cache)
Comments
Post a Comment