wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes working with the DataBase Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "working with the DataBase" Watch "working with the DataBase" New topic
Author

working with the DataBase

Matt Pavlovich
Ranch Hand

Joined: Aug 14, 2010
Posts: 98
Does anyone know of a good tutorial on working with a database such as the one we have with the URLyBird project? I understand the ObjectOutputStream and ObjectInputStream classes, but what is really getting me is reading the byte[]. For instance, I know that the Magic Cookie in my database is 4 bytes, because that is what my instructions state. And so in Roberto's FileReader, we have:


This reads the byte array and and prints out the value of the magic cookie. But how exactly does this work? How do we know where we are at in this file? When we call the read() method on the byte[magicCookieByteArray] , we are reading an array of 4 bytes, but it seems to me that could be any array with 4 bytes, right? Wow I am confused... I think what is throwing me for a loop is the question of how do we know where we are at in the file, and how do we move to a specific place in the database to read or write to it?

Thank you all,
Matt
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5139
    
  12

Hi Matt,

I used a RandomAccessFile for reading/writing from/to a database file. I created for myself also a database file reader and it does not use the read with byte[] (for reading numbers) because that's more work. Instead I used methods like readInt and readShort.

Here is the program. It might require a bit more tweaking because it was especially developed for my database file:
When using a RandomAccessFile you can use seek-method to position the file pointer on a given offset. Using the getFilePointer-method gives you the current offset in the file.

Hope it helps!
Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Matt Pavlovich
Ranch Hand

Joined: Aug 14, 2010
Posts: 98
Roel,

Man you are my hero. Thank you! That's a lot clearer to my little greenhorn brain. To make sure I am understanding this correctly, the readInt() method will read each line, 4 bytes at a time, and after the end of the line is reached (in the first case after the magicCookie value has been read), it will stop. Is that right?

Also, the use of the RandomAccessFile to modify and read it makes sense. When using the seek() method, though, can I only go to either the start or the end of the file? (I am guessing I can do more). The API docs state I should define the offset position, measured in bytes from the beginning of the file, at which to set the file pointer. However, how exactly do I go about determining how many bytes it is to record number 3 for example?

And finally, when I get to the point where I am going to be locking a record, am I locking onto a byte[] object? In other words, each line in the "database" is a byte array...correct?

Thank you again.
Matt
Matt Pavlovich
Ranch Hand

Joined: Aug 14, 2010
Posts: 98
Ureka! I think I have it. The length of the Record (for me at 159) is how many bytes long each element of the byte array is. So if I need to get to record three, the math is pretty easy. Now it makes more sense how I need to access this thing.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5139
    
  12

Hi Matt,

Your explanation about RandomAccessFile is not completely correct. Maybe you should have a close look to its API and how random access files work.

The readInt() method will read just 4 bytes and return an int. You use this method when you want to read a signed int. But it doesn't proceed to the next line, it just reads 4 bytes. When you use the readLine() method it will read everything on that line until a line terminator or end of file is reached. But all this explanation you can easily find in the javadoc of RAF.

With seek() you can go to any location in your file, not just start and/or end. So you can easily jump to 3th record and overwrite this record because it was update. So you don't have to go through all data before this 3rd record. Like with a CD: you jump to number 5 and play that song without having to listen to crappy songs 1 until 4

So when you want to write something to your file, you'll code will be something like this:

You have to make sure that both actions are executed as an atomic operation, otherwise some other thread can alter the position of the file pointer and your bytes will be written on a different location.

How to determine the position of record number 3? That's your homework for tonight, it's not that hard and I don't want to spoil your fun

Kind regards,
Roel
Matt Pavlovich
Ranch Hand

Joined: Aug 14, 2010
Posts: 98
Roel,

Thanks very much for your explanation. Yes I dove into Murach's Java SE6, and now the whole issue, as well as how to use the seek() method to get to specific records, is a lot clearer.

Thank you again for your help and patience.
Matt
Matt Pavlovich
Ranch Hand

Joined: Aug 14, 2010
Posts: 98
hmmm...I am working with the file now using the RandomAccessFile class. Am I correct in stating that we cannot write to this file - that it is locked? All I can do is read from it. Even when I just try to define it as "rw" I get "Access is denied". Just want to make sure I am not making an invalid assumption.

Thanks.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5139
    
  12

No, that's not correct!

From the javadoc:
"rw" --> Open for reading and writing. If the file does not already exist then an attempt will be made to create it.


Maybe some kind of file permissions issue? Trying to write to a read-only file.
Matt Pavlovich
Ranch Hand

Joined: Aug 14, 2010
Posts: 98
Okay, I fixed the problem. I originally had it stored in the C: drive, which requires Admin approval before changes can be made. I moved it to the desktop and now I can write to it. I thought it was strange not to be able to write to it.

I'm off to try to organize this bad boy and make some sense of it. Thank you as always for your help.
Matt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: working with the DataBase
 
Similar Threads
Database file reader
NX:Please analyze the DBAccess interface
B&S: Beginner Questions
Interpretation of data file format
NX:[URLyBird]Get the correct field values