Using Random Access Files
Hey all
so i have to create a random access file that i have to both write and read from, and it will be filled with records consisting of an integer (key field, between 0 and 9999), a double, and a string. (this will all go into a .dat file)
Okay so what i would do first usually is basically create 9999 blank records, and i have no problem doing this with just the integer and double.
I'll show you the code i have for creating blank records of just an integer(account number) and double (balance)
also note: when i say "Key field" i mean the field that will be used when i search for a record.
RandomAccessFile myFile =new RandomAccessFile("theFile.dat","rw")
finalint RECORDSIZE = 12;//size of an integer is 4, size of a double is 8
finalint NUMRECORDS = 9999;
try{
for(int x = 0; x < NUMRECORDS; ++x)
{
myFile.writeInt(0);
myFile.writeDouble(0.0);
}
}
catch(IOException e){
System.out.println("Error: " + e.getMessage());
}
Right so, my problem:
When i want to add in a string, i have to use writeUTF() right? i'm not sure on either how to make a "blank" area for a string, since it could be of variable bytes. i've tried experimenting with a few things like in that loop there putting myFile.writeUTF(" "); but that obviously doesn't work.
so basically what i need is a way to, instead of having:
Int Double Int Double Int Double......... all the way through the dat file
i need to have:
Int String Double Int String Double Int String Double...
Any help would be appreciated, thanks
Edit: I think in simpler terms i think i'm trying to create a fixed length .dat file
Message was edited by:
Adam123

