File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes data access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "data access" Watch "data access" New topic
Author

data access

Shengzhi Peng
Greenhorn

Joined: Jun 16, 2005
Posts: 10
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

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Shengzhi Peng
Greenhorn

Joined: Jun 16, 2005
Posts: 10
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

Joined: Mar 28, 2003
Posts: 11460
    
  94

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


    The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
    Shengzhi Peng
    Greenhorn

    Joined: Jun 16, 2005
    Posts: 10
    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

    Joined: Jun 16, 2005
    Posts: 10
    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

    Joined: Mar 28, 2003
    Posts: 11460
        
      94

    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

    Joined: Jun 16, 2005
    Posts: 10
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: data access