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;
}
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;
}
相关文章推荐
- (vc)分享一个读写ini文件的类,支持多种数据类型的读写,二进制数据都能保存和读取
- 【转帖】通过PHP读取dbf数据(visual fox pro,VFP数据库),官方的dbase无法读取字段为类型memo的数据,国外网站的解决方案 How to read FoxPro Memo with PHP?
- SDI文件打开对话框的文档过滤器(支持多种类型的实现)
- opencv支持多种流行视频格式文件的读取
- 【Java】运用多种方式读取文件内容,包括按字节、字符和按行为单位读取文件内容
- opencv支持多种流行视频格式文件的读取
- 一款轻量级的文件选择器,支持多种文件类型
- opencv支持多种流行视频格式文件的读取
- net控件中数据导到Excel的格式 首先,我们了解一下excel从web页面上导出的原理。当我们把这些数据发送到客户端时,我们想让客户端程序(浏览器)以excel的格式读取它,所以把mime类型设为:application/vnd.ms-excel,当excel读取文件时会以每个cell的格式呈现数据,如果cell没有规定的格式,则excel会以默认的格式去呈现该cell的数据。这样就给我们提供了自定义数据格式的空间,当然我们必须使用excel支持的格式。下面就列出常用的一些格式: 1) 文本
- C#中读取“已注册的文件类型”的图标及读取指定文件图标的方法
- sql语句读取dbf文件_sql语句_数据库开发
- 【Java/Android性能优5】 Android ImageCache图片缓存,使用简单,支持预取,支持多种缓存算法,支持不同网络类型,扩展性强
- OpenCV读取一张图片的Mat数据类型到.txt文件
- 类:CTexture,OpenGL中读取多种纹理的类。支持bmp,tga,rgb,rgba等
- 让Dreamweaver支持phtml(支持更多的文件类型)
- java 文件 读取目录下的所有文件(包括子目录)
- Visual Studio某些项目类型不能使用ConfigurationManager读取app.config文件的解决办法
- C# 读取app.config配置文件 节点键值,提示 "System.TypeInitializationException 类型初始值设定项引发异常" 错误的解决方案
- 读取DBF文件数据
- matlab中怎样读取一个文件夹里面的指定类型的所有文件的名称?