`
小嘴看世界
  • 浏览: 129592 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

Java操作dbf文件

    博客分类:
  • Java
阅读更多

dbf算是个古老的数据文件了,使用Java操作起来感觉不怎么方便,不过有了javadbf这个jar一切问题解决了。

下载地址为:http://sarovar.org/projects/javadbf/

并且官方有比较详细的Tutorial

就是这里啦 http://sarovar.org/docman/view.php/32/23/javadbf-tutorial.html

JavaDBF Library Tutorial

(for JavaDBF ver. 0.4.0 and above)
Anil Kumar K.
anil at linuxense dot com
Linuxense Information Systems Pvt. Ltd., Trivandrum, India

1. Introduction

JavaDBF is a Java library for reading and writing XBase files. There are plenty of legacy applications around with .dbf as their primary storage format. JavaDBF was initially written for data transfer with such applications.

Other than that, there are requirements to export data from a Java application to a spreadshet program like GNumeric, Excel or Lotus 123. A DBF file would be more appropriate in such situations rather than a CSV or an HTML file because a DBF file can carry field type information. More over, XBase format is like an Open-standard; it is understood by almost all spreadsheet programms.

2. Getting and Installing

Obtain the latest version of JavaDBF from http://sarovar.org/projects/javadbf/. Create a folder in a convenient location and run:

	tar xvfz javadbf-x.x.x-tar.gz 
	cd javadbf-x.x.x

In this folder you will find javadbf.jar which contains the library. Include this jar file in your $CLASSPATH variable. You are ready to go.

3. Overview of the Library

JavaDBF has a simple API of its own and it does not implement the JDBC API. It is designed this way because JavaDBF is not indedned to support full-blown RDBMS-style database interaction. And you are not supposed to use it like a back-end; it just doesn't work that way. Also, JavaDBF is not designed to be thread-safe; keep that in mind when you design threaded applications.

JavaDBF comes in the package com.linuxense.javadbf. Import that package in your Java code. Following examples will familiarise you with its APIs.

3.1. Data Type Mapping

In version 0.3.2, JavaDBF supports almost all XBase data types except Memo field. While reading, those types are interpretted as appropriate Java types. Following table shows the mapping scheme.

XBase Type XBase Symbol Java Type used in JavaDBF
Character C java.lang.String
Numeric N java.lang.Double
Double F lava.lang.Double
Logical L java.lang.Boolean
Date D java.util.Date

4. Reading a DBF File

To read a DBF file, JavaDBF provides a DBFReader class. Following is a ready-to-compile, self-explanatory program describing almost all feature of the DBFReader class. Copy/paste this listing and compile it. Keep a .dbf file handy to pass to this program as its argument.

import java.io.*;
import com.linuxense.javadbf.*;

public class JavaDBFReaderTest {

  public static void main( String args[]) {

    try {

      // create a DBFReader object
      //
      InputStream inputStream  = new FileInputStream( args[ 0]); // take dbf file as program argument
      DBFReader reader = new DBFReader( inputStream); 

      // get the field count if you want for some reasons like the following
      //
      int numberOfFields = reader.getFieldCount();

      // use this count to fetch all field information
      // if required
      //
      for( int i=0; i<numberOfFields; i++) {

        DBFField field = reader.getField( i);

        // do something with it if you want
        // refer the JavaDoc API reference for more details
        //
        System.out.println( field.getName());
      }

      // Now, lets us start reading the rows
      //
      Object []rowObjects;

      while( (rowObjects = reader.nextRecord()) != null) {

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

          System.out.println( rowObjects[i]);
        }
      }

      // By now, we have itereated through all of the rows
      
      inputStream.close();
    }
    catch( DBFException e) {

      System.out.println( e.getMessage());
    }
    catch( IOException e) {

      System.out.println( e.getMessage());
    }
  }  
}  

5. Writing a DBF File

The class complementary to DBFReader is the DBFWriter.While creating a .dbf data file you will have to deal with two aspects: 1. define the fields and 2. populate data. As mentioned above a dbf field is represented by the class DBFField. First, let us familiarise this class.

5.1. Defining Fields

Create an object of DBFField class:

  DBFField field = new DBFField();
  field.setField( "emp_name"); // give a name to the field
  field.setDataType( DBFField.FIELD_TYPE_C); // and set its type
  field.setFieldLength( 25); // and length of the field

This is, now, a complete DBFField Object ready to use. We have to create as many DBFField Objects as we want to be in the .dbf file. The DBFWriter class accept DBFField in an array. Now, let's move on to the next step of populating data.

5.2. Preparing DBFWriter Object.

A DBFWriter is used for creating a .dbf file. First lets create a DBFWriter object by calling its constructor and then set the fields created (as explained above) by calling the setFields method.

DBFWriter writer = new DBFWriter();
writer.setFields( fields); // fields is a non-empty array of DBFField objects

Now, the DBFWriter Object is ready to be populated. The method for adding data to the DBFWriter is addRecord and it takes an Object array as its argument. This Object array is supposed contain values for the fields added with one-to-one correspondence with the fields set.

Following is a complete program explaining all the steps described above:

import com.linuxense.javadbf.*;
import java.io.*;

public class DBFWriterTest {

  public static void main( String args[])
  throws DBFException, IOException {

    // let us create field definitions first
    // we will go for 3 fields
    //
    DBFField fields[] = new DBFField[ 3];

    fields[0] = new DBFField();
    fields[0].setName( "emp_code");
    fields[0].setDataType( DBFField.FIELD_TYPE_C);
    fields[0].setFieldLength( 10);

    fields[1] = new DBFField();
    fields[1].setField( "emp_name");
    fields[1].setDataType( DBFField.FIELD_TYPE_C);
    fields[1].setFieldLength( 20);

    fields[2] = new DBFField();
    fields[2].setField( "salary");
    fields[2].setDataType( DBFField.FIELD_TYPE_N);
    fields[2].setFieldLength( 12);
    fields[2].setDecimalCount( 2);

    DBFWriter writer = new DBFWriter();
    writer.setFields( fields);

    // now populate DBFWriter
    //

    Object rowData[] = new Object[3];
    rowData[0] = "1000";
    rowData[1] = "John";
    rowData[2] = new Double( 5000.00);

    writer.addRecord( rowData);

    rowData = new Object[3];
    rowData[0] = "1001";
    rowData[1] = "Lalit";
    rowData[2] = new Double( 3400.00);

    writer.addRecord( rowData);

    rowData = new Object[3];
    rowData[0] = "1002";
    rowData[1] = "Rohit";
    rowData[2] = new Double( 7350.00);

    writer.addRecord( rowData);

    FileOutputStream fos = new FileOutputStream( args[0]);
    writer.write( fos);
    fos.close();
  }
}

Keep in mind that till the write method is called, all the added data will be kept in memory. So, if you are planning to write huge amount of data make sure that it will be safely held in memory till it is written to disk and the DBFWriter object is garbage-collected. Read the ``Sync Mode'' section to know how JavaDBF to use a special feature of JavaDBF to overcome this.

5.3. ``Sync Mode'' --Writing Records to File as They are Added

This is useful when JavaDBF is used to create a DBF with very large number of records. In this mode, instead of keeping records in memory for writing them once for all, records are written to file as addRecord() is called. Here is how to write in Sync Mode:

Create DBFWriter instance by passing a File object which represents a new/non-existent or empty file. And you are done! But, as in the normal mode, remember to call write() when have added all the records. This will help JavaDBF to write the meta data with correct values. Here is a sample code:

import com.linuxense.javadbf.*;
import java.io.*;

public class DBFWriterTest {

  public static void main( String args[])
  throws DBFException, IOException {

    // ...

    DBFWriter writer = new DBFWriter( new File( "/path/to/a/new/file")); /* this DBFWriter object is now in Syc Mode */
    // ...
  }
}	

7. Appending Records

From version 0.4.0 onwards JavaDBF supports appending of records to an existing DBF file. Use the same constructor used in Sync Mode to achieve this. But here the File object passed to the construction should represent the DBF file to which records are to be appended.

It is illegal to call setFields in DBFWriter object created for appending. Here also it is required to call the write() method after adding all the records.

6. Planned Features

  1. Support for memo fields.

© 2003, 2004 Anil Kumar Krishnan Nair, Linuxense

分享到:
评论
3 楼 bhdweb 2010-07-16  
不行呀` 不支持memo类型的字段,楼主有解决的方式么
2 楼 justry 2010-04-17  
我的汉字怎么存进去之后是乱码呢?
1 楼 justry 2010-04-17  
歌,
我的程序怎么运行到dbfWriter.addRecord(data);这句就空指针啊.

相关推荐

    java操作dbf文件

    包括java读写dbf文件源码,可改写重用,可用于生成dbf文件或解析dbf文件获取数据。

    通过java操作dbf文件的javadbf

    通过java操作dbf文件的javadbf,含源码,api

    Java操作DBF文件的API

    java操作DBF文件的API zip格式,包括jar包和doc

    java操作dbf文件jar包(源码+说明)

    java操作dbf的jar包,连同源码和帮助文档, 修复导出中文乱码问题;

    用JavaDBF操作(读、写)DBF文件

    用JavaDBF操作(读、写)DBF文件

    java解析dbf文件三种方法、以及解析驱动

    用java解析dbf文件,三种方法,一种按行解析,另两种是要把dbf当做一个表来进行操作就像查询一样 解析驱动是把dbf文件当做表来解析,两种方法中其中一种要装驱动,另一种不要装,这取决你的dbf文件的格式

    javadbfjar.zip

    java操作dbf文件读写用的jar包。1.9版本。例子: package dbf; import java.io.FileInputStream; import java.io.InputStream; import com.linuxense.javadbf.DBFField; import com.linuxense.javadbf.DBFReader; ...

    LINUX平台JAVA直接连接access数据库dbf文件

    JAVA直接连接access数据库dbf文件,同时兼容LINUX、UNIX、WINDOWS操作系统。示例工程代码,导入eclipse即可运行。

    基于javadbf-0[1].4.0.jar包的dbf文件操作源码

    一般的dbf文件都是数据库操作文件,用c语言进行操作的dll很多,但是java操作的很少,我找到了一个javadbf-0[1].4.0.jar的包,但是操作效果很不理想,尤其是末尾空格和乱码问题,于是我在这个jar包的基础上进行进一步...

    javadbf.4.1.jar

    javadbf.jar是java用来读取操作dbf文件的包,此包版本为4.1,经过优化,实测可用。

    javadbf-1.9.4.jar

    javadbf.jar是java用来读取操作dbf文件的依赖包,maven仓库中只能找到0.4.0版本,好不容易找到1.9.4版本,供大家下载使用

    DBF操作实例

    利用Javadbf.jar包操作dbf文件的实例

    DBF文件操作示例, 里面已经追加了相关的测试数据哦:), 不容错过!!!

    很好的DBF文件操作示例, 里面已经追加了相关的测试数据哦:), 不容错过!!!

    javadbf-1.8.0.jar

    pdf文件的内容的解析和和内容的提取入库操作,其中pdf的解析采用了开源的apache pdfbox 插件,版本选用的是最新版本的1.8.0版本

    基于java实现的数据库管理系统.7z

    相当于把.java编译成了.class,再读取dbf文件,执行实体对dbf文件内容进行相应的操作,相当于 类加载执行,实现sql语句的解析执行的过程。 2.实现了create,insert,update,delete,select,多表查询,排序等简单...

    Java解析XML工具类--(java源码)

    * 本类是专门解析XML文件的,主要用于为系统读取自己的配置文件时提供最方便的解析操作 * @author HX * */ public class XmlManager { /** * 得到某节点下某个属性的值 * @param element 要获取属性的...

    sun.jdbc.odbc.JdbcOdbcDriver下载

    jdbc-odbc驱动包,提示找不到驱动,java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver, 原因:从jdk从1.8开始,删除了jdbc-odbc桥,所以odbc的驱动是用不了的 解决方法:这里为提供一个解决JDK1.8不...

    网管教程 从入门到精通软件篇.txt

    DBF:dBASE文件,一种由Ashton-Tate创建的格式,可以被ACT!、Lipper、FoxPro、Arago、Wordtech、Xbase和类似数据库或与数据库有关产品识别;可用数据文件(能被Excel 97打开);Oracle 8.1.x表格空间文件 DBX:...

    彩虹UDA软件狗工具带硬复制工具

    2 对数据库文件 (PRC 、 FOX 、 APP 、 DBF), 利用配套软件中的加密工具加密。 3 对于 C 语言及其它编译型语言,配套软件中提供可链接的模块文件 (OBJ 文件 ) , 模块文件中提供两个函数 :(1) 写数据 ; ⑵读数据。...

    DBRECOVER for Oracle:dul数据卸载程序,恢复损坏的oracle数据库,删除,取消运行-开源

    PRM支持从9i,10g,11g到19c的Oracle数据库PRM可以在损坏的文件系统,ASM DiskGroup和数据文件上工作。 。 即使Oracle数据字典丢失,PRM也可以基于不一致的SYSTEM.DBF表空间备份来扫描和恢复字典。 PRM可以支持...

Global site tag (gtag.js) - Google Analytics