java对DBF的Memo字段操作

本文介绍如何扩展javadbf-0.4.0.jar包的DBFReader类以支持DBF文件中的memo字段读取。通过新增构造函数及方法处理memo字段数据,并提供关键代码示例。

最近做一个DBF数据导入,下载了javadbf-0.4.0.jar包,发现少了对memo字段读入的支持,决定对这个包的DBFReader类进行扩展,实现对memo字段的读入。

memo字段的存储格式:

/*备注头记录  
   
   字节偏移   说明    
   00   -   03   下一个自由块的位置1    
   04   –   05   未使用    
   06   –   07   块大小(每个块的字节数)1    
   08   –   511   未使用    
   1   存储整数时,高位字节在前。    
    
    
   备注块标头与备注文本  
    
   字节偏移   说明    
   00   –   03   块签名   1   (指示块中数据的类型)  
   0   –   图片(图片字段类型)  
   1   –   文本(备注字段类型)    
   04   –   07   备注长度   1   (以字节为单位)    
   08   –   n   备注文本(n   =   长度)    
   1   存储整数时,高位字节在前。     */

构建一个新的构造函数

public DBFReader( InputStream in, InputStream memo) throws DBFException {
  this(in);

// 读入memo内容
  try {
   this.memoInputStream = new DataInputStream(memo);
   this.initMemoInputStream();// 初始化数据流,代码见下面
  } catch (IOException e) {
   throw new DBFException( e.getMessage()); 
  }
 }

private void initMemoInputStream() throws IOException {
  memoInputStream.read(new byte[6]); // 00   -   03   下一个自由块的位置1       04   –   05   未使用
  blockLength = Utils.readHighEndianShort(memoInputStream); //  06   –   07   块大小(每个块的字节数)1
  memoInputStream.read(new byte[504]); // 08   –   511   未使用
 }

 在nextRecord()方法中,

case 'M':
      // TODO Later

这个位置进行memo字段处理,加了如下代码:

case 'M':
      // TODO Later
      // DBF跳过memo字段
      byte b_memoProxy[] = new byte[ this.header.fieldArray[i].getFieldLength()];
      dataInputStream.read( b_memoProxy);
      // 读取FPT字段
      int iType = Utils.readHighEndianInt(memoInputStream);
      if (iType == 1) {
       int iLength = Utils.readHighEndianInt(memoInputStream);// 红色代码为自己添加的函数
       byte b_memo[] = new byte[ iLength];
       memoInputStream.read(b_memo); // 读入的FPT文件内容
       recordObjects[i] = new String( b_memo, characterSetName);
       if (iLength + 8 > blockLength) {
        memoInputStream.read(new byte[blockLength - (iLength + 8) % blockLength]);
       } else {
        memoInputStream.read(new byte[blockLength - 8 - iLength]);
       }
      }
      break;

此时,完成了memo字段的读入,下面附上readHighEndianInt的代码,在Utils类中实现

public static int readHighEndianInt( DataInput in)
 throws IOException {

  int bigEndian = 0;
  for( int shiftBy=24; shiftBy>-1; shiftBy-=8) {

   bigEndian |= (in.readUnsignedByte()&0xff) << shiftBy;
  }

  return bigEndian;
 }

 

public static short readHighEndianShort( DataInput in)
 throws IOException {

  int high = in.readUnsignedByte();
  int low = in.readUnsignedByte() & 0xff;

  return (short )(high << 8 | low);
 }

到此,读入memo工作全部完成。如果按照这种步骤做下来还有问题,需要源代码,请留言。

要使用MFC ADO读写DBF文件中的memo字段,可按以下步骤操作: ### 初始化OLE/COM库环境 ADO库是一组COM动态库,在调用ADO前,需要初始化OLE/COM库环境。在MFC应用程序里,可在应用程序主类的`InitInstance`成员函数里完成此操作: ```cpp BOOL CYourApp::InitInstance() { // 初始化OLE/COM库环境 CoInitialize(NULL); // 其他初始化代码 return TRUE; } ``` 在程序结束时,需要调用`CoUninitialize`释放COM库资源,可在`ExitInstance`函数中实现: ```cpp int CYourApp::ExitInstance() { // 释放COM库资源 CoUninitialize(); return CWinApp::ExitInstance(); } ``` ### 建立数据库连接 使用ADO连接到DBF文件所在的目录,示例代码如下: ```cpp _ConnectionPtr pConn(__uuidof(Connection)); try { // 连接字符串,指定DBF文件所在目录 CString strConn = _T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\YourDBFDirectory;Extended Properties=dBASE IV;"); pConn->Open(_bstr_t(strConn), _T(""), _T(""), adModeUnknown); } catch (_com_error& e) { // 处理连接错误 AfxMessageBox(e.ErrorMessage()); } ``` ### 读取memo字段 通过执行SQL查询语句读取DBF文件中的数据,示例代码如下: ```cpp _RecordsetPtr pRs(__uuidof(Recordset)); try { // 执行查询语句 pRs->Open(_T("SELECT * FROM YourDBFFile.dbf"), pConn.GetInterfacePtr(), adOpenStatic, adLockOptimistic, adCmdText); while (!pRs->adoEOF) { // 获取memo字段的值 _variant_t varMemo = pRs->Fields->GetItem(_T("MemoFieldName"))->Value; if (varMemo.vt != VT_NULL) { CString strMemo = (LPCTSTR)_bstr_t(varMemo); // 处理memo字段的值 } pRs->MoveNext(); } pRs->Close(); } catch (_com_error& e) { // 处理查询错误 AfxMessageBox(e.ErrorMessage()); } ``` ### 写入memo字段 通过执行SQL插入或更新语句写入数据到DBF文件的memo字段,示例代码如下: ```cpp try { CString strMemoValue = _T("This is a memo field value."); CString strSQL = _T("INSERT INTO YourDBFFile.dbf (MemoFieldName) VALUES ('") + strMemoValue + _T("')"); pConn->Execute(_bstr_t(strSQL), NULL, adCmdText); } catch (_com_error& e) { // 处理写入错误 AfxMessageBox(e.ErrorMessage()); } ``` ### 关闭数据库连接 在操作完成后,关闭数据库连接: ```cpp if (pConn->State == adStateOpen) { pConn->Close(); } ``` ### 注意事项 - 确保DBF文件所在目录有读写权限。 - 不同版本的DBF文件可能需要不同的OLE DB提供程序和扩展属性,上述示例使用的是`Microsoft.Jet.OLEDB.4.0`和`dBASE IV`。 - 在处理`_variant_t`类型的值时,需要注意空值的情况。
评论 31
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值