• 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

About the db file question.

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone:
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
Repeated read each field in a record,
My codes follows:
System.out.println(db.readInt()); //magic cookie value
System.out.println(db.readInt()); //offset to start of record zero
short ii=db.readShort(); //number of fields in each record
System.out.println(ii);
for (short i=0;i<ii ;i++ )
{
System.out.println(db.readShort()); //length in bytes of field name
How to write codes for "n bytes (defined by previous entry), field name " and "2 byte numeric, field length in bytes "
}
Give me a idea for write this codes.
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jofer
Create a byte array of size n and use the readFully method of RandomAccessFile to read the field name into this array (as binary). Then pass the byte array to the appropriate constructor of the String class (make sure you specify the correct character encoding).
The 2 byte numeric (field length in bytes) - you already know how to do this, you did it in the code sample you posted above.
Hope this helps
Michael
[ October 21, 2003: Message edited by: Michael Fitzmaurice ]
[ October 21, 2003: Message edited by: Michael Fitzmaurice ]
 
Jofer Chan
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,Michael :
Thank you,I have understood.
but the new question occur,I write the codes is follow:
import java.io.*;
import java.util.*;
class TestRandomAccessFile
{
private static int headerLen;
private static int recordLen = 1;
private static int recordCount;
private static RandomAccessFile db;
public static void main(String[] args)
throws IOException
{
File f = new File(args[0]);
byte byteArray[] = null;
if (f.exists() && f.canRead() && f.canWrite()) {
db = new RandomAccessFile(f, "r");
System.out.println(db.readInt());
int iii=db.readInt();
System.out.println(iii);
short ii=db.readShort();
System.out.println(ii);
short lengthShort[] = new short[ii];
for (short i=0;i<ii ;i++ )
{
int a = db.readShort();
System.out.println(a);
byteArray = new byte[a];
db.readFully(byteArray);
System.out.println(new String(byteArray));
lengthShort[i] = db.readShort();
System.out.println(lengthShort[i]);
}
for (int i=0;i<iii ;i++ )
{
System.out.print(db.readShort()+" ");
for (int aa=0;aa<lengthShort.length ;aa++ )
{
byte temp[] = new byte[lengthShort[aa]];
db.readFully(temp);
System.out.print(new String(temp)+" ");
}
System.out.println();
}
} else {
throw new IOException("Data: request to open non-existant or " +
"inaccessible file" + args[0]);
}
}
}
the above codes can read all date(information) is right.
but occur Exception follow:

514
70
6
4
name
32
8
location
64
11
specialties
64
4
size
6
4
rate
8
5
owner
8
0 Dogs With Tools Smallville //more date(field)in a record
0 Hamner & Tong Smallville ......
0 Philharmonic Remodeling Whoville ......
0 Fred & Nobby Whoville ......
0 Dogs With Tools Whoville ......
0 Dogs With Tools Metropolis ......
0 Bitter Homes & Gardens Metropolis ......
0 Philharmonic Remodeling Metropolis ......
0 Dogs With Tools Pleasantville ......
0 Moore Power Tool Ya Pleasantville ......
0 Swanders & Flaughn Digitopolis ......
0 Fred & Nobby Digitopolis ......
0 Hamner & Tong Atlantis ......
0 Moore Power Tool Ya Atlantis ......
0 Hamner & Tong EmeraldCity ......
0 Bitter Homes & Gardens EmeraldCity ......
0 Dogs With Tools Bali Hai
0 Fred & Nobby Bali Hai ......
0 Hamner & Tong Xanadu ......
0 Moore Power Tool Ya Xanadu ......
0 Philharmonic Remodeling Xanadu ......
0 Buonarotti & Company Paravel ......
0 Hamner & Tong Paravel ......
0 Philharmonic Remodeling Hobbiton ......
0 Buonarotti & Company Hobbiton ......
0 Dogs With Tools Lendmarch ......
0 Buonarotti & Company Lendmarch ......
0 Swanders & Flaughn Lendmarch ......
Exception in thread "main" java.io.EOFException
at java.io.RandomAccessFile.readShort(Unknown Source)
at TestRandomAccessFile.main(TestRandomAccessFile.java:39)

The format of data in the database file is as follows:
Start of file
4 byte numeric, magic cookie value identifies this as a data file
4 byte numeric, offset to start of record zero
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. (offset into file equal to "offset to start of record zero" value)
Repeat to end of file:
2 byte flag. 00 implies valid record, 0x8000 implies deleted record
Record containing fields in order specified in schema section, no separators between fields, each field fixed length at maximum specified in schema information
End of file

How to solved?
 
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jofer,
Your java.io.EOFException simply signals that your file has an end and that you reached its end. All good news !
There are two ways of doing :
  • Or you avoid the EOFException, by dynamically computing the highest possible record number (maxRecNo = (fileSize - headerSize) / recordLength + 1) before reading / updating / deleting, and you throw RecordNotFoundException if the recNo passed as parameter is greater than your computed maxRecNo.
  • Or you catch EOFException and throw a RecordNotFoundException from within the catch block.


  • Best,
    Phil.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic