• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

B&S Database File Question

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
hangman
Posts: 220
Angular Framework Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was the ".db" that I had left out!!! Thank you! I now feel extremely stupid.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic