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")

final int RECORDSIZE = 12; //size of an integer is 4, size of a double is 8

final int 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

[1796 byte] By [Adam123a] at [2007-11-27 8:37:21]
# 1

Do you have any reason to stick with a fixed length file? Random Access Files are hard to work with and very slow compared to writing/reading a plain text delimited file. It's easy to create and read a delimited file. For example to read a comma delimited file here's all you need (untested code),

...

try{

String fileName="c:\myfile.txt";

File f = new File(fileName);

String s;

String[] lineTemp;

BufferedFileReader br = new BufferedFileReader(new FileReader(f));

while((s=br.readLine()!)=null){

lineTemp[]=s.split(",");

//now do something with the array of values from this row of data

//such as add values to a TableModel

}

}catch(IOException ex){

ex.printStackTrace();

}

...

If you want to make sure that doubles are cast as doubles and Strings are cast as string you could extend AbstractTableModel, especially if you don't know in advance the type of data in each column. If the data type in each column is know then just use a method like parseDouble() to convert to a double.

Writing the delimited file is just as easy. From my experience all this extra code still works faster than reading/writing froma RandomAccessFile.

Jstatsa at 2007-7-12 20:34:39 > top of Java-index,Java Essentials,Java Programming...
# 2
Unfortuanately it needs to be a Random Access .dat file, i agree, its terribly annoying to work with, but its what i have to do
Adam123a at 2007-7-12 20:34:39 > top of Java-index,Java Essentials,Java Programming...
# 3

========RESOLVED=========

Solution:

Well, i think it was a solution, anyway.

basically what i did was to use another writeInt(0) method to reserve space for technically an integer, but then instead used writeChars("StringHere") into that integer space, then used readChar for each char, and it seems to work fine.

Thanks for your help!

Adam123a at 2007-7-12 20:34:39 > top of Java-index,Java Essentials,Java Programming...