/** * @param dbfFile A DBF file * @return A list of attribute names in the DBF file */ public static List<String> getAttributeNames(File dbfFile) throws IOException { FileInputStream fis = new FileInputStream(dbfFile); DbaseFileReader dbfReader = new DbaseFileReader(fis.getChannel(), false, Charset.forName("ISO-8859-1")); // contains the header columns DbaseFileHeader dbfHeader = dbfReader.getHeader(); // get the names from the header List<String> names = new Vector<String>(); int n = dbfHeader.getNumFields(); for (int i = 0; i < n; i++) names.add(dbfHeader.getFieldName(i)); return names; }
// returns a string format of the SQL Datatype of the column using the getFieldType function from // DBaseFileHeader private static String getSQLDataType(Connection conn, DbaseFileHeader dbfHeader, int index) { char dataType = dbfHeader.getFieldType(index); String sqlDataType = ""; if (dataType == 'C') sqlDataType = SQLUtils.getVarcharTypeString(conn, dbfHeader.getFieldLength(index)); else if (dataType == 'N' || dataType == 'F') { // if it has not 0 decimals return type as integer else Double Precision if (dbfHeader.getFieldDecimalCount(index) == 0) sqlDataType = SQLUtils.getBigIntTypeString(conn); else sqlDataType = SQLUtils.getDoubleTypeString(conn); } else if (dataType == 'D') { sqlDataType = SQLUtils.getDateTimeTypeString(conn); } else { throw new RuntimeException( "Unknown DBF data type: " + dataType + " in column " + dbfHeader.getFieldName(index)); } return sqlDataType; }
/** * @param dbfFile A DBF file * @return A list of attribute names in the DBF file */ public static Object[][] getDBFData(File dbfFile) throws IOException { FileInputStream fis = new FileInputStream(dbfFile); DbaseFileReader dbfReader = new DbaseFileReader(fis.getChannel(), false, Charset.forName("ISO-8859-1")); // contains the header columns DbaseFileHeader dbfHeader = dbfReader.getHeader(); List<Object[]> rowsList = new Vector<Object[]>(); while (dbfReader.hasNext()) { rowsList.add(dbfReader.readEntry()); } int numOfCol = dbfHeader.getNumFields(); Object[][] dataRows = new Object[rowsList.size()][numOfCol]; for (int i = 0; i < rowsList.size(); i++) { dataRows[i] = rowsList.get(i); } return dataRows; }