• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Should we always use RandomAccessFile

 
Kannan Ekanath
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am coding for my SCJD assignment and following APRESS book. My question is the one available in this Javaranch topic.

The assignment clearly says that "All numeric values are stored in the header information use the formats of the DataInputStream and DataOutputStream classes. All text values, and all fields (which are text only), contain only 8 bit characters, null terminated if less than the maximum length for the field. The character encoding is 8 bit US ASCII".

1) Does this mean that we should use only DataInputStream and Output streams? Can I not use RandomAccessFile (since it is quite easy to implement?).
2) If I have to use the DataInputStream and DataOutputStream then does someone has samples on how to read and write data to same file using two different streams please?
 
Jeffry Kristianto Yanuar
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To make Accessing database file easier, we should use RandomAccessFile, because it can seek to the certain byte location back and forward. I think it is the best approach for accessing database file easier. So, use it. Simplicity is the key for passing this tough exam.

Good luck !!! and wish me luck too!!!

Jeffry Kristianto Yanuar (Java Instructor)
SCJP 5.0, SCJA, SCJD (UrlyBird 1.3.2) --> Waiting for the result
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, my buddy.

1) Does this mean that we should use only DataInputStream and Output streams? Can I not use RandomAccessFile (since it is quite easy to implement?).


Partner, if you look at the DataInputStream class API, you'll see that it implements the DataInput interface; the DataOutputStream class implements the DataOutput interface and the RandomAccessFile class implements both of them. So yes, you can use RandomAccessFile class too as well.

2) If I have to use the DataInputStream and DataOutputStream then does someone has samples on how to read and write data to same file using two different streams please?


You can use the RandomAccessFile class

Just a curiosity, here's what I did: when my application starts, I read all the database file and load all records to a HashMap, where the record number is the key and the Room object (a class with the same structure as the database, without the record number) is the value. And this is what I control throughout the application. When the application finishes, I write all records back to the database file. Since I "may assume that at any moment, at most one program is accessing the database file", and the number of records is not big, then I thought this would be the simplest approach. So I deal with the physical .db file only 2 times: when the application starts, and when it finishes.
[ November 26, 2008: Message edited by: Roberto Perillo ]
 
Uros Mesaric
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding DataInputStream and RandomAccessFile. Why not use both. I use both of them. I read header data with first and record data with other. Use what best solves the problem.

About managing data in memory. I don't think it is good idea. I think you should write data back to file when data changes. Server might run several months and data changes need to be persisted to DB file, don't you thin so.
I am having the same problems with this data caching. What if you have millions of records, do you say in sys requirements that it needs 64 bit JVM!?
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if you have millions of records


I justified that, given the number of records, and the machines from nowadays, I did not see that as a problem. I said that, since they didn't say anything about that, I assumed that the database would never grow to a point that keeping these records in memory would be a problem. I think that opening and closing the data file every time a change occurs is a problem, since I/O operations are the most expensive ones, in terms of CPU execution clock cycles.
[ November 26, 2008: Message edited by: Roberto Perillo ]
 
Kim Lauwers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think both options are great.

1) Keeping in memory (that's what I do)
or
2) Always closing and opening the file.

It just depends on how you motivate your choice in choices.txt.

I motivate, that I think the database will never grow to that size, It's to big to keep in memory.

Greetz,
Kim
 
Uros Mesaric
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using RandomAccessFile, you can write changes back really fast.
What will happen if during test or real life somebody kills your server process? Transactions will not be persisted!
 
Jeffry Kristianto Yanuar
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roberto used similar technique that covered in "SCJD Exam with J2SE". When the application start, the server loads all the records and store in HashMap.

I have another solution, instead putting all the records (What if the amount of the records is 2 million?), I used a certain formula to go to the particular record. Because the database contains fixed record, there always be a formula to calculate where the byte location for the certain record number.

Both the solutions is good, but be sure to document your solution.

hope that will help.
Good luck on your exam and wish me luck too !!

Jeffry Kristianto Yanuar (Java Instructor)
SCJP 5.0, SCJA, SCJD (UrlyBird 1.3.2) --> Waiting for the result
[ November 27, 2008: Message edited by: Jeffry Kristianto Yanuar ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic