This week's book giveaway is in the Java in General forum. We're giving away four copies of 97 Things Every Java Programmer Should Know and have Kevlin Henney & Trisha Gee on-line! See this thread for details.
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?
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
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 ]
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!?
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 ]
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.
Programming today is a race between Software Engineers striving to build bigger and better idiot-proof Programs, and the Universe trying to produce bigger and better idiots.<br />So far, the Universe is winning!
posted 11 years ago
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
posted 11 years ago
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 ]
Replace the word "snake" with "danger noodle" in all tiny ads.
Devious Experiments for a Truly Passive Greenhouse!