Class DbaseFileReader

Object
DbaseFileReader
All Implemented Interfaces:
Closeable, AutoCloseable, FileReader
Direct Known Subclasses:
IndexedDbaseFileReader

public class DbaseFileReader extends Object implements FileReader, Closeable
A DbaseFileReader is used to read a dbase III format file.
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();

 
Author:
Ian Schneider, Andrea Aaime
  • Field Details

    • useMemoryMappedBuffer

      protected boolean useMemoryMappedBuffer
    • randomAccessEnabled

      protected boolean randomAccessEnabled
    • currentOffset

      protected long currentOffset
  • Constructor Details

  • Method Details

    • fill

      protected int fill(ByteBuffer buffer, ReadableByteChannel channel) throws IOException
      Throws:
      IOException
    • getHeader

      public DbaseFileHeader getHeader()
      Get 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
      Clean up all resources associated with this reader.Highly recomended.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException - If an error occurs.
    • hasNext

      public boolean hasNext()
      Query the reader as to whether there is another record.
      Returns:
      True if more records exist, false otherwise.
    • readEntry

      public Object[] readEntry() throws IOException
      Get the next record (entry). 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
      Throws:
      IOException
    • skip

      public void skip() throws IOException
      Skip the next record.
      Throws:
      IOException - If an error occurs.
    • readEntry

      public Object[] readEntry(Object[] entry, int offset) throws IOException
      Copy the next record into the array starting at offset.
      Parameters:
      entry - Th array to copy into.
      offset - The offset to start at
      Returns:
      The same array passed in.
      Throws:
      IOException - If an error occurs.
    • readField

      public Object readField(int fieldNum) throws IOException
      Reads a single field from the current record and returns it. Remember to call read() before starting to read fields from the dbf, and call it every time you need to move to the next record.
      Parameters:
      fieldNum - The field number to be read (zero based)
      Returns:
      The value of the field
      Throws:
      IOException - If an error occurs.
    • transferTo

      public void transferTo(DbaseFileWriter writer) throws IOException
      Transfer, by bytes, the next record to the writer.
      Throws:
      IOException
    • read

      public void read() throws IOException
      Reads the next record into memory. You need to use this directly when reading only a subset of the fields using readField(int).
      Throws:
      IOException
    • readEntry

      public Object[] readEntry(Object[] entry) throws IOException
      Copy the next entry into the array.
      Parameters:
      entry - The array to copy into.
      Returns:
      The same array passed in.
      Throws:
      IOException - If an error occurs.
    • main

      public static void main(String[] args) throws Exception
      Throws:
      Exception
    • id

      public String id()
      Description copied from interface: FileReader
      An id for the reader. This is only used for debugging.
      Specified by:
      id in interface FileReader
      Returns:
      id for the reader.