Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

db file read error?

 
Jashua Ni
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anybody encounter read errors when read from original db file?

I could not get the magic number, length of record in bytes, etc. in the header?

Any suggestions?

Thanks.

Jashua Ni
 
Jashua Ni
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought I could replicate this file(which I did during the tesign), the instruction says only the original file is to be used. But there is no magic number, length of recod in bytes, and number of fields in a record. If I read them in through bytes, then convert them using BigInteger, I got 0 for all of them.

Any suggestions? Thanks.

Jashua Ni
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to read the magic number, and other non-text information in the data file, using the readShort() method (or similar) from the java.io.DataInput interface (i.e. via instances of the DataInputStream or RandomAccessFile classes), unless I'm completely missing the point. I'm not sure how this translates if you're using NIO.

Hope this helps

Jules
 
Jashua Ni
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, Julian:

I did use RandomAccessFile to read, if I used the method readShort(), an exception will be thrown, says end of file, I also tried to used DataInputStream to read, got same thing, after I read from there through bytes, I can see the 4 byte of '\u0000'. If I open the file in text editor, I could only see wired like square box in the beginning.

Can you see your db file clearly in a text editor?

Thanks a lot and please help,

Jashua Ni
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jashua,

Actually I've just reread my own code and I use readInt() to get the magic number, etc. I initially tried readShort() and just got '\u0000' and then tried readChar() assuming that it needed to be a 16-bit unsigned integer, but got the same result. So, rather unintuitively, I've used readInt() and it works. That's probably the cause of your woes.

Another possibility that springs to mind is that when you create a new RandomAccessFile in "rw" mode, does it create an empty file if the specified path and file name does not exist? That would also cause the symptom you're seeing. Check that the path and file name you specify in your code are 100% correct.

If you still have trouble, try the following code, which is a crude reader that I knocked up when I first got hold of the database file. Make sure that you change "db-2x2.db" to be the fully qualified path and file name of your database file.




Text editors typically show non-ASCII characters as little square blocks and mine is no different. My file shows 7 of them then an F, then 4 more and then "name". If you have Visual Studio on your machine you can open the file in binary mode and actually see the data contents as hex. I'm sure there are other binary-capable file editors too but I don't know of another off hand.

Good luck! :-)

Jules
 
Jashua Ni
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Julian:

Thank you so much. OK, what I found out is, I copy the file to another machine, and some how got changed. I was doing alomost same as you did, except mode to be "rw", no it's OK. Thank again for your so detail explanation. I appreicated.

Jashua Ni
 
Mingwei Jiang
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jashua,
You can use UltraEdit to see contents in binary mode, and it is also a great test editor, and it's rather small to install.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic