您的位置:首页 > 编程语言 > Java开发

javadbf 读取dbf文件 支持包括memo的多种类型

2013-05-23 17:15 363 查看
下面是读取dbf中各种数据类型,包括memo类型的部分源码

public Comparable[] nextRecord(Comparable[] recordObjects) 
throws DBFException {

if( isClosed) {

throw new DBFException( "Source is not open");
}

if (recordObjects == null)
recordObjects = new Comparable[ this.header.fieldArray.length];

try {

wasDeleted = false;
do {

if( wasDeleted) {

dataInputStream.skip( this.header.recordLength-1);
}

int t_byte = dataInputStream.readByte();
if( t_byte == END_OF_DATA) {

return null;
}

wasDeleted = (  t_byte == '*');
} while( skipDeleted && wasDeleted);

for( int i=0; i<recordObjects.length; i++) {

Byte type = this.header.fieldArray[i].getDataType();
if ( type == DBFField.FIELD_TYPE_C) 
{

byte b_array[] = new byte[ this.header.fieldArray[i].getFieldLength()];
dataInputStream.read( b_array);
int end_index = Utils.trimRightSpaces( b_array, 0);
recordObjects[i] = new String(b_array, 0, end_index, characterSetName);
if ("".equals(recordObjects[i])) {
recordObjects[i] = null;
}
}
else if ( type == DBFField.FIELD_SQLTYPE_TIMESTAMP ||
type == DBFField.FIELD_SQLTYPE_TIME ||
type == DBFField.FIELD_SQLTYPE_DATE ||
type == DBFField.FIELD_TYPE_D) 
{

byte t_byte_year[] = new byte[ 4];
dataInputStream.read( t_byte_year);

byte t_byte_month[] = new byte[ 2];
dataInputStream.read( t_byte_month);

byte t_byte_day[] = new byte[ 2];
dataInputStream.read( t_byte_day);

try {

GregorianCalendar calendar = new GregorianCalendar( 
Integer.parseInt( new String( t_byte_year)),
Integer.parseInt( new String( t_byte_month)) - 1,
Integer.parseInt( new String( t_byte_day))
);
long m = calendar.getTimeInMillis();
if (type == DBFField.FIELD_TYPE_D)
recordObjects[i] = new Date(m);
else if (type == DBFField.FIELD_SQLTYPE_TIMESTAMP)
recordObjects[i] = new java.sql.Timestamp(m);
else if (type == DBFField.FIELD_SQLTYPE_TIME)
recordObjects[i] = new java.sql.Time(m);
else if (type == DBFField.FIELD_SQLTYPE_DATE)
recordObjects[i] = new java.sql.Date(m);
}
catch ( NumberFormatException e) {
/* this field may be empty or may have improper value set */
recordObjects[i] = null;
}
}
else if (type ==  DBFField.FIELD_TYPE_F ||
type == DBFField.FIELD_TYPE_N)
{
byte t_numeric[] = new byte[ this.header.fieldArray[i].getFieldLength()];
dataInputStream.read( t_numeric);
int start_index = Utils.trimLeftSpaces( t_numeric);
int end_index = Utils.trimRightSpaces( t_numeric, start_index);
if (end_index == 0 || Utils.contains( t_numeric, start_index, end_index, (byte)'?')) {
recordObjects[i] = null;
}
else if (Utils.isTemplate( t_numeric, start_index, end_index)) {
recordObjects[i] = BigDecimal.ZERO;
}
else {
String num = new String(t_numeric, start_index, end_index-start_index);
try {
recordObjects[i] = new BigDecimal(num);
}
catch( NumberFormatException e) {
// throw new DBFException( "Failed to parse BigDecimal-'"+num+"' column-"+(i+1)+" ("+this.header.fieldArray[i].getName()+")");
System.out.println( "Failed to parse BigDecimal-'"+num+"' column-"+(i+1)+" ("+this.header.fieldArray[i].getName()+")");
}
}
}
else if ( type == DBFField.FIELD_LANGTYP
b978
E_SHORT) {
byte t_numeric[] = new byte[ this.header.fieldArray[i].getFieldLength()];
dataInputStream.read( t_numeric);
int start_index = Utils.trimLeftSpaces( t_numeric);
int end_index = Utils.trimRightSpaces( t_numeric, start_index);
if (end_index == 0 || Utils.contains( t_numeric, start_index, end_index, (byte)'?')) {
recordObjects[i] = null;
}
else if (Utils.isTemplate( t_numeric, start_index, end_index)) {
recordObjects[i] = ShortZERO;
}
else {
String num = new String(t_numeric, start_index, end_index-start_index);
try {
recordObjects[i] = new Short(num);
}
catch (NumberFormatException e) {
throw new DBFException( "Failed to parse Short-'"+num+"' column-"+(i+1));
}
}
}
else if ( type == DBFField.FIELD_LANGTYPE_INTEGER) {
byte t_numeric[] = new byte[ this.header.fieldArray[i].getFieldLength()];
dataInputStream.read( t_numeric);
int start_index = Utils.trimLeftSpaces( t_numeric);
int end_index = Utils.trimRightSpaces( t_numeric, start_index);
if (end_index == 0 || Utils.contains( t_numeric, start_index, end_index, (byte)'?')) {
recordObjects[i] = null;
}
else if (Utils.isTemplate( t_numeric, start_index, end_index)) {
recordObjects[i] = IntegerZERO;
}
else {
String num = new String(t_numeric, start_index, end_index-start_index);
try {
recordObjects[i] = new Integer(num);
}
catch (NumberFormatException e) {
throw new DBFException( "Failed to parse Integer-'"+num+"' column-"+(i+1));
}
}
}
else if ( type == DBFField.FIELD_LANGTYPE_LONG) {
byte t_numeric[] = new byte[ this.header.fieldArray[i].getFieldLength()];
dataInputStream.read( t_numeric);
int start_index = Utils.trimLeftSpaces( t_numeric);
int end_index = Utils.trimRightSpaces( t_numeric, start_index);
if (end_index == 0 || Utils.contains( t_numeric, start_index, end_index, (byte)'?')) {
recordObjects[i] = null;
}
else if (Utils.isTemplate( t_numeric, start_index, end_index)) {
recordObjects[i] = LongZERO;
}
else {
String num = new String(t_numeric, start_index, end_index-start_index);
try {
recordObjects[i] = new Long(num);
}
catch( NumberFormatException e) {
throw new DBFException( "Failed to parse Long-'"+num+"' column-"+(i+1));
}
}
}
else if ( type == DBFField.FIELD_TYPE_L) {

byte t_logical = dataInputStream.readByte();
if( t_logical == 'Y' || t_logical == 't' || t_logical == 'T' || t_logical == 't') {

recordObjects[i] = Boolean.TRUE;
}
else {

recordObjects[i] = Boolean.FALSE;
}
}
else if (type ==  DBFField.FIELD_TYPE_M) {
int fieldLength=this.header.fieldArray[i].getFieldLength();
byte t_memoptr[] = new byte[fieldLength ];
dataInputStream.read( t_memoptr);
if (memoFile != null) {
if (10 == fieldLength) {
int start_index = Utils.trimLeftSpaces(t_memoptr);
int end_index = Utils.trimRightSpaces(t_memoptr,

start_index);
if (end_index == 0
|| Utils.contains(t_memoptr, start_index,
end_index, (byte) '?')) {
recordObjects[i] = null;
} else if (Utils.isTemplate(t_memoptr, start_index,
end_index)) {
recordObjects[i] = null;
} else {
String num = new String(t_memoptr, start_index,
end_index - start_index);
try {
recordObjects[i] = readMemo(new Long(num).longValue());
} catch (NumberFormatException e) {
throw new DBFException("Failed to parse Memo-'"
+ num + "' column-" + (i + 1));
} catch (IOException e) {
throw new DBFException(
"Failed to read Memo");
}
}
} else if (4 == fieldLength) {
long offset=((int)t_memoptr[0] & 0xFF)+
(((int)t_memoptr[1] & 0xFF)<<8)+
(((int)t_memoptr[2] & 0xFF)<<16)+
(((int)t_memoptr[3] & 0xFF)<<24);
if (offset!=0) {
recordObjects[i] = readMemo(offset);
}
}
}
}
else {
throw new DBFException("Unsupported field type:'"+this.header.fieldArray[i].getDataType()+"'");
}
}
}
catch( EOFException e) {

return null;
}
catch( IOException io) {

throw new DBFException( io);
}

return recordObjects;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java javadbf memo
相关文章推荐