Package org.geotools.data.shapefile.dbf
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: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).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();
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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classDbaseFileReader.Row
-
Field Summary
Fields Modifier and Type Field Description protected longcurrentOffsetprotected booleanrandomAccessEnabledprotected booleanuseMemoryMappedBuffer
-
Constructor Summary
Constructors Constructor Description DbaseFileReader(ReadableByteChannel readChannel, boolean useMemoryMappedBuffer, Charset charset)DbaseFileReader(ReadableByteChannel readChannel, boolean useMemoryMappedBuffer, Charset charset, TimeZone timeZone)DbaseFileReader(ShpFiles shapefileFiles, boolean useMemoryMappedBuffer, Charset charset)DbaseFileReader(ShpFiles shapefileFiles, boolean useMemoryMappedBuffer, Charset charset, TimeZone timeZone)Creates a new instance of DBaseFileReader
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Clean up all resources associated with this reader.Highly recomended.protected intfill(ByteBuffer buffer, ReadableByteChannel channel)DbaseFileHeadergetHeader()Get the header from this file.booleanhasNext()Query the reader as to whether there is another record.Stringid()An id for the reader.static voidmain(String[] args)voidread()Reads the next record into memory.Object[]readEntry()Get the next record (entry).Object[]readEntry(Object[] entry)Copy the next entry into the array.Object[]readEntry(Object[] entry, int offset)Copy the next record into the array starting at offset.ObjectreadField(int fieldNum)Reads a single field from the current record and returns it.DbaseFileReader.RowreadRow()voidskip()Skip the next record.voidtransferTo(DbaseFileWriter writer)Transfer, by bytes, the next record to the writer.
-
-
-
Constructor Detail
-
DbaseFileReader
public DbaseFileReader(ShpFiles shapefileFiles, boolean useMemoryMappedBuffer, Charset charset, TimeZone timeZone) throws IOException
Creates a new instance of DBaseFileReader- Parameters:
shapefileFiles- The readable channel to use.- Throws:
IOException- If an error occurs while initializing.
-
DbaseFileReader
public DbaseFileReader(ShpFiles shapefileFiles, boolean useMemoryMappedBuffer, Charset charset) throws IOException
- Throws:
IOException
-
DbaseFileReader
public DbaseFileReader(ReadableByteChannel readChannel, boolean useMemoryMappedBuffer, Charset charset) throws IOException
- Throws:
IOException
-
DbaseFileReader
public DbaseFileReader(ReadableByteChannel readChannel, boolean useMemoryMappedBuffer, Charset charset, TimeZone timeZone) throws IOException
- Throws:
IOException
-
-
Method Detail
-
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 IOExceptionClean up all resources associated with this reader.Highly recomended.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- 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 IOExceptionSkip 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 callread()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 IOExceptionReads the next record into memory. You need to use this directly when reading only a subset of the fields usingreadField(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.
-
id
public String id()
Description copied from interface:FileReaderAn id for the reader. This is only used for debugging.- Specified by:
idin interfaceFileReader- Returns:
- id for the reader.
-
-