Dbf reader in java

Dbf reader in java

Construct an object out of the specified shapefile’s DBF file input stream and of the optional CPG file input stream.

Method Summary

Methods inherited from class java.lang.Object

Constructor Detail

DBFReader

public DBFReader(java.io.InputStream inputStream, java.io.InputStream cpgFileInputStream) throws java.io.IOException

Construct an object out of the specified shapefile’s DBF file input stream and of the optional CPG file input stream.

DBFReader

public DBFReader(java.io.InputStream inputStream) throws java.io.IOException

Method Detail

numRecords

numFields

recordSize

getRecord

public byte[] getRecord(int nth)

getFieldData

public java.lang.String getFieldData(int ith, byte[] rec)

Returns the value string for the ith field in the given record. Returns null is returned if something is wrong (e.g., invalid field index). If the .cpg file is available then the method transform the string from the original encoding to UTF-8.

getFieldName

public java.lang.String getFieldName(int ith)

getFieldType

public byte getFieldType(int ith)

Returns the type for the ith field in a record. returns a single character the meaning of which is defined as below:

'C': character 'D': Date (8 digits: YYYYMMDD) 'F': Floating point binary numeric: -. 0 1 2 3 4 5 6 7 8 9 'G': General. All OEM characters or OLE. 'L': Logical: ? Y y N n T t F f (? initially) 'M': Memo 'N': Float: - . 0 1 2 3 4 5 6 7 8 9 'I': Integer: - 0 1 2 3 4 5 6 7 8 9

print

Источник

Читайте также:  What is generator object python

Dbf reader in java

Construct an object out of the specified shapefile’s DBF file input stream and of the optional CPG file input stream.

Method Summary

Methods inherited from class java.lang.Object

Constructor Detail

DBFReader

public DBFReader(java.io.InputStream inputStream, java.io.InputStream cpgFileInputStream) throws java.io.IOException

Construct an object out of the specified shapefile’s DBF file input stream and of the optional CPG file input stream.

DBFReader

public DBFReader(java.io.InputStream inputStream) throws java.io.IOException

Method Detail

numRecords

numFields

recordSize

getRecord

public byte[] getRecord(int nth)

getFieldData

public java.lang.String getFieldData(int ith, byte[] rec)

Returns the value string for the ith field in the given record. Returns null is returned if something is wrong (e.g., invalid field index). If the .cpg file is available then the method transform the string from the original encoding to UTF-8.

getFieldName

public java.lang.String getFieldName(int ith)

getFieldType

public byte getFieldType(int ith)

Returns the type for the ith field in a record. returns a single character the meaning of which is defined as below:

'C': character 'D': Date (8 digits: YYYYMMDD) 'F': Floating point binary numeric: -. 0 1 2 3 4 5 6 7 8 9 'G': General. All OEM characters or OLE. 'L': Logical: ? Y y N n T t F f (? initially) 'M': Memo 'N': Float: - . 0 1 2 3 4 5 6 7 8 9 'I': Integer: - 0 1 2 3 4 5 6 7 8 9

Источник

Dbf reader in java

Construct an object out of the specified shapefile’s DBF file input stream and of the optional CPG file input stream.

Method Summary

Methods inherited from class java.lang.Object

Constructor Detail

DBFReader

public DBFReader(java.io.InputStream inputStream) throws java.io.IOException

DBFReader

public DBFReader(java.io.InputStream inputStream, java.io.InputStream cpgFileInputStream) throws java.io.IOException

Construct an object out of the specified shapefile’s DBF file input stream and of the optional CPG file input stream.

Method Detail

getFieldData

public java.lang.String getFieldData(int ith, byte[] rec)

Returns the value string for the ith field in the given record. Returns null is returned if something is wrong (e.g., invalid field index). If the .cpg file is available then the method transform the string from the original encoding to UTF-8.

getFieldName

public java.lang.String getFieldName(int ith)

getFieldType

public byte getFieldType(int ith)

Returns the type for the ith field in a record. returns a single character the meaning of which is defined as below:

'C': character 'D': Date (8 digits: YYYYMMDD) 'F': Floating point binary numeric: -. 0 1 2 3 4 5 6 7 8 9 'G': General. All OEM characters or OLE. 'L': Logical: ? Y y N n T t F f (? initially) 'M': Memo 'N': Float: - . 0 1 2 3 4 5 6 7 8 9 'I': Integer: - 0 1 2 3 4 5 6 7 8 9

getRecord

public byte[] getRecord(int nth)

numFields

numRecords

recordSize

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Java library for fast reading DBF-files.

License

jamel/dbf

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Java library for fast reading/writing DBF-files.

For build project from sources you need to run gradlew script from the root directory:

git clone git@github.com:jamel/dbf.git cd dbf ./gradlew clean build

Maven artifact is available from maven central repository. Just add dependency in your pom.xml:

dependency> groupId>org.jamel.dbfgroupId> artifactId>dbf-readerartifactId> version>0.0.3version> dependency>
DBF field type Returned as
character (C) for reduce memory consumption and improve performance it returns as byte[] . If you need to get a String, use the appropriate constructor of String class.
date (D) java.util.Date
float (F) java.lang.Float
logical (L) java.lang.Boolean
numeric (N) java.lang.Number

1. Loading data from small dbf files into collection

In the next example we load data from streets.dbf mapping each row to objects of Street class:

public class LoadStreetsExample < public static void main(String[] args) < File dbf = new File("streets.dbf"); ListStreet> streets = DbfProcessor.loadData(dbf, new DbfRowMapperStreet>() < @Override public Street mapRow(Object[] row) < // here we can change string encoding if it is needed String name = new String(DbfUtils.trimLeftSpaces((byte[]) row[0])); Number zip = (Number) row[1]; Date createdAt = (Date) row[2]; return new Street(name, zip.intValue(), createdAt); > >); System.out.println("Streets: " + streets); > >

2. Processing each row form dbf file

In the next example we calculate total sum and average price of data from products.dbf :

public class PricesCalcExampleV1 < public static void main(String[] args) < File dbf = new File("products.dbf"); TotalSumCalculator calc = new TotalSumCalculator(); DbfProcessor.processDbf(dbf, calc); System.out.println("Total sum: " + calc.getTotalSum()); System.out.println("Average price: " + calc.getTotalSum() / calc.getRowsCount()); > private static class TotalSumCalculator implements DbfRowProcessor < private double totalSum; private int rowsCount; @Override public void processRow(Object[] row) < // assuming that there are prices in the 4th column totalSum += ((Number) row[3]).doubleValue(); rowsCount++; > private double getTotalSum() < return totalSum; > private int getRowsCount() < return rowsCount; > > >

3. Print general information of DBF-file

public class DbfInfo < public static void main(String[] args) < String dbfInfo = DbfProcessor.readDbfInfo(new File("altnames.dbf")) System.out.println(dbfInfo); > >
Created at: 13-7-15 Total records: 39906 Header length: 129 Columns: # Name Type Length Decimal --------------------------------------------- 0 OLDCODE C 19 0 1 NEWCODE C 19 0 2 LEVEL C 1 0 

4. Manually processing rows (low level API)

In the next example we again calculate total sum and average price of data from products.dbf . But this time we will manually create DbfReader and iterate throughout each row:

public class PricesCalcExampleV2 < public static void main(String[] args) < try (DbfReader reader = new DbfReader(new File("products.dbf"))) < double totalSum = 0; Object[] row; while ((row = reader.nextRecord()) != null) < // assuming that there are prices in the 4th column totalSum += ((Number) row[3]).doubleValue(); > System.out.println("Total sum: " + totalSum); System.out.println("Average price: " + totalSum / reader.getHeader().getNumberOfRecords()); > > >

To get column values you can also use ResultSet-like API. The while loop from the last example can be rewritten like this:

DbfRow row; while ((row = reader.nextRow()) != null) < totalSum += row.getDouble("Price"); >

5. Translate DBF to TXT file

If you have no tool for viewing DBF fields you could simply output all its content to txt file and use your favorite text editor.

public class Dbf2Txt < public static void main(String[] args) < DbfProcessor.writeToTxtFile( new File("altnames.dbf"), new File("altnames.txt"), Charset.forName("cp866")); > >

6. Read DBF records at a specified indices

To read one or more records at specific positions in the DBF without iterating through all records, you will have to use the DbfReader constructor which takes a File as an argument.

public class RandomRecordAccess < public static void main(String[] args) < try (DbfReader reader = new DbfReader(new File("multiple-records.dbf"))) < // read the tenth record (the indices are zero-based) reader.seekToRecord(9); Object[] record = reader.nextRecord(); // process the record . // read the third record reader.seekToRecord(2); record = reader.nextRecord(); // process the record . > > >

Dbf writing functionality currently is not available.

Источник

Оцените статью