Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

data access

 
Shengzhi Peng
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
4 byte numeric, magic cookie value. Identifies this as a data file
4 byte numeric, total overall length in bytes of each record
2 byte numeric, number of fields in each record

Schema description section.
Repeated for each field in a record:
2 byte numeric, length in bytes of field name
n bytes (defined by previous entry), field name
2 byte numeric, field length in bytes
end of repeating block

Data section.
Repeat to end of file:
1 byte "deleted" flag. 0 implies valid record, 1 implies deleted record

My data accessing code :

Is this right?What should i modify?Thank you for your response.

[ July 17, 2005: Message edited by: Shengzhi Peng ]

[Andrew: put code between [code] and [/code] UBB tags]
[ July 17, 2005: Message edited by: Andrew Monkhouse ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you want to use the readUTF method of RandomAccessFile. You have to read a number of bytes specified by one of your readShort calls. Check the RandomAccessFile API again and see if you can find a method that can read n bytes into a buffer (an array), and then you can convert the result to a String.
 
Shengzhi Peng
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.Even if you told me the answer.I would still think that if it is the best way.This is my way to do everything.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11914
209
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shengzhi,

You should really try to use descriptive names for your variables. For example
  • magicCookie instead of s1
  • recordSize instead of s2
  • numberOfFields instead of s3
  • You have only shown the code for reading the schema - is that intentional?

    As Barry mentioned, using readUTF (at least in the way you are using it) is probably not correct. Take a look at the API documentation for readUTF - how does this method know the amount of data to read?

    Regards, Andrew
     
    Shengzhi Peng
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you for your response.The problem is that i don't know how to access exact number of bytes.I have read the API.And do not know what method is suitable.
     
    Shengzhi Peng
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    try
    {
    FieldInfo field[] = null;
    File file =new File("db-2x1.db");
    RandomAccessFile db = new RandomAccessFile(file,"rw");

    int magicCookie = db.readInt();
    int recordSize = db.readInt();
    int numberOfFields = db.readShort();

    for(int i = 0; i < s3; i++)
    {
    int fieldLength = db.readShort();
    field[i] = new FieldInfo(db.readUTF(),db.readShort);
    }
    }
    Like this, OK?But how to use fieldLength to influence the readUTF()?
    May i know what method i should use? Thank you !
     
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander
    Pie
    Posts: 11914
    209
    C++ Firefox Browser IntelliJ IDE Java Mac Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Shengzhi,

    Please make sure that code you are posting will at least compile, otherwise I get the feeling you are asking me to do your research for you.

    From the API documentation for readUTF:
    The first two bytes are read, starting from the current file pointer, as if by readUnsignedShort. This value gives the number of following bytes that are in the encoded string, not the length of the resulting string. The following bytes are then interpreted as bytes encoding characters in the UTF-8 format and are converted into characters.
    So the readUTF method itself reads the size of the field - you should not be doing this within your program.

    Regards, Andrew
     
    Shengzhi Peng
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Andrew Monkhouse
    Thank you for your response.I really don't how to search the record from the file.This have stopped me two days to write the code.Maybe you will think if you told me how to do.I will not think it myself,it is not good for me.But i think the answer will not stop me to think it myself.I just want to know the basic way to do so.And i will turn the knowledge to myself.
    Thank you.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic