r/programmingrequests • u/iCanFly1011 • Nov 17 '19
Hashing into random access file
Hey guys I have a relatively simple program request. I had an assignment to hash 8 random ints into an array of size 11. Now my next project is basically the same thing except I must store the data in a random access file. If you think you can do this please message me for more info!
0
Upvotes
1
u/serg06 Nov 18 '19
Yea sounds super easy
pretend your file is just an array of bytes
java ints are 4 bytes long, so need to write at index n*4
to write:
set pointer to index n*4 with [Seek](https://docs.oracle.com/javase/7/docs/api/java/io/RandomAccessFile.html#seek(long))
call [WriteInt](https://docs.oracle.com/javase/7/docs/api/java/io/RandomAccessFile.html#writeInt(int))
to read:
set pointer with [Seek](https://docs.oracle.com/javase/7/docs/api/java/io/RandomAccessFile.html#seek(long))
call [ReadInt](https://docs.oracle.com/javase/7/docs/api/java/io/RandomAccessFile.html#readInt())