jwo.utils.dbase
Class DbaseFileReader

java.lang.Object
  extended by jwo.utils.dbase.DbaseFileReader

public class DbaseFileReader
extends Object

Used to read Dbase III files. This code is based on the class provided as part of the Geotools OpenSource mapping toolkit - http://www.geotools.org/ under the GNU Lesser General Public License. The general use of this class is:

 FileChannel in = new FileInputStream("thefile.dbf").getChannel();
 DbaseFileReader r = new DbaseFileReader( in )
 Object[] fields = new Object[r.getHeader().getNumFields()];
 while (r.hasNext()) 
 {
    r.readEntry(fields);
    // do stuff
 }
 r.close();
 
For consumers who wish to be a bit more selective with their reading of rows, the Row object has been added. The semantics are the same as using the readEntry method, but remember that the Row object is always the same. The values are parsed as they are read, so it pays to copy them out (as each call to Row.read() will result in an expensive String parse).
EACH CALL TO readEntry OR readRow ADVANCES THE FILE!
An example of using the Row method of reading:
 FileChannel in = new FileInputStream("thefile.dbf").getChannel();
 DbaseFileReader r = new DbaseFileReader(in)
 int fields = r.getHeader().getNumFields();
 while (r.hasNext()) 
 {
   DbaseFileReader.Row row = r.readRow();
   for (int i = 0; i < fields; i++) 
   {
     // do stuff
     Foo.bar(row.read(i));
   }
 }
 r.close();
 

Version:
2.3, 11th April 2006.
Author:
Ian Schneider with minor modifications by Jo Wood.

Nested Class Summary
 class DbaseFileReader.Row
          Stores an individual row in the database.
 
Constructor Summary
DbaseFileReader(ReadableByteChannel channel)
          Creates a new instance of DBaseFileReader.
DbaseFileReader(ReadableByteChannel channel, Logger logger)
          Creates a new instance of DBaseFileReader with warning messages reported to the given logger.
 
Method Summary
 void close()
          Cleans up all resources associated with this reader.
 DbaseFileHeader getHeader()
          Retrieves the header from this file.
 boolean hasNext()
          Queries the reader as to whether there is another record.
 Object[] readEntry()
          Retrieves the next record (entry).
 Object[] readEntry(Object[] entry, boolean doSimple)
          Copies the next entry into the array.
 Object[] readEntry(Object[] entry, int offset, boolean doSimple)
          Copies the next record into the array starting at offset.
 DbaseFileReader.Row readRow()
          Reads the next row from the database.
 Object[] readSimpleEntry()
          Retrieves the next record (entry), formatted as a collection of numbers and/or strings.
 void skip()
          Skips the next record.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DbaseFileReader

public DbaseFileReader(ReadableByteChannel channel)
                throws IOException
Creates a new instance of DBaseFileReader.

Parameters:
channel - The readable channel to use.
Throws:
IOException - If an error occurs while initializing.

DbaseFileReader

public DbaseFileReader(ReadableByteChannel channel,
                       Logger logger)
                throws IOException
Creates a new instance of DBaseFileReader with warning messages reported to the given logger.

Parameters:
channel - The readable channel to use.
logger - Logger to monitor warning messages.
Throws:
IOException - If an error occurs while initializing.
Method Detail

getHeader

public DbaseFileHeader getHeader()
Retrieves the header from this file. The header is read upon instantiation.

Returns:
The header associated with this file or null if an error occurred.

close

public void close()
           throws IOException
Cleans up all resources associated with this reader. Should be called after all required data has been extracted from the database.

Throws:
IOException - If an error occurs.

hasNext

public boolean hasNext()
Queries the reader as to whether there is another record.

Returns:
True if more records exist, false otherwise.

readEntry

public Object[] readEntry()
                   throws IOException
Retrieves the next record (entry). Will return a new array of values.

Returns:
A new array of values.
Throws:
IOException - If an error occurs.

readSimpleEntry

public Object[] readSimpleEntry()
                         throws IOException
Retrieves the next record (entry), formatted as a collection of numbers and/or strings. Will return a new array of values.

Returns:
A new array of values.
Throws:
IOException - If an error occurs.

readRow

public DbaseFileReader.Row readRow()
                            throws IOException
Reads the next row from the database. Advances the file pointer to the start of the next record.

Returns:
Next row in the database.
Throws:
IOException - if error occurs when reading.

skip

public void skip()
          throws IOException
Skips the next record.

Throws:
IOException - if an error occurs.

readEntry

public Object[] readEntry(Object[] entry,
                          int offset,
                          boolean doSimple)
                   throws IOException
Copies the next record into the array starting at offset.

Parameters:
entry - The array to copy into.
offset - The offset to start at.
doSimple - Will format as strings/numbers if true.
Returns:
The same array passed in.
Throws:
IOException - id an error occurs.

readEntry

public Object[] readEntry(Object[] entry,
                          boolean doSimple)
                   throws IOException
Copies the next entry into the array.

Parameters:
entry - The array to copy into.
doSimple - Will format as strings/numbers if true.
Returns:
The same array passed in.
Throws:
IOException - If an error occurs when trying to read entry from database.


Copyright Jo Wood, 1996-2009, last modified, 17th April, 2009