Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

B&S Database File Question

 
Katie McGettrick
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is another beginner question I'm afraid. I've been messing around with RandomAccessFiles - I've never really used them so I'm just trying to get a bit more comfortable with them.

Anyhow, what I'm doing is very very simple at the moment - I create a RandomAccessFile with the name of my database. However, when I try to do anything with it it seems to be empty. I've tried printing out file.length() which is zero, and any of the read() methods throw and exception saying I've passed the end of file.

Anyone else had this problem?
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the access mode in your constructor? Could you please post a fragment of your code?
 
Katie McGettrick
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said, I'm not actually writing code for my project yet, I'm just messing around to get the feel for random access files so I can work on a solution. Here's an example of something I've tried:



The output of the above code is: 0 Where "filename" is the location of my db file provided by sun.

The filename is correct by the way - it is actually creating the file but when I try to do anything with it it acts as if the file is empty (it isn't). I've tried using various RandomAccessFile methods like read(), I just used the .length example above as it is the simplest example I can think of.
[ May 09, 2006: Message edited by: Katie McGettrick ]
 
Bob Nedwor
hangman
Ranch Hand
Posts: 215
Eclipse IDE Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure your file is not "db-2x2.db" with the .db extension? My project had a .db extension.

You can always make a copy of the file, then try to open it in a text editor and see if there is anything in there. When I did this, I saw the data, but also a lot of encoded characters.

I used the other constructor, where I passed in an object of type "File", not "String" as my first argument when I instanciated RandomAccessFile.

Assuming your datafile is in a directory called c:\myproject\datafile, try something like this:


This is just a temporary solution to show you how the RandomAccessFile class works. Eventually you will have to figure out how to get the path and filename from the user, via a GUI interface, and pass that into your File constructor.

Please see the official rules for building a RandomAccessFile.
[ May 09, 2006: Message edited by: Bob Nedwor ]
 
Katie McGettrick
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was the ".db" that I had left out!!! Thank you! I now feel extremely stupid.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need to feel stupid, but make sure you make some backups of your original file before writing to it! You have to send the unmodified file with the assignment submission.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic