`
小嘴看世界
  • 浏览: 131764 次
  • 性别: 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是一种常见的数据库文件格式,常见于FoxPro、dBase等早期数据库管理系统中。在Java中处理DBF文件,主要是为了兼容这些旧系统的数据或者进行特定的数据交换。下面...

    Java操作DBF文件的API

    Java操作DBF文件的API是Java开发者用于读取和写入DBF(dBase文件格式)数据的重要工具。DBF是一种常见的数据库文件格式,广泛应用于早期的桌面数据库系统,如dBase、FoxPro等。在Java中处理这些文件通常需要第三方库...

    通过java操作dbf文件的javadbf

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

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

    Java操作DBF文件是数据库处理中的一个常见需求,尤其是在与老式系统集成或者处理遗留数据时。DBF文件是Dbase、FoxPro等早期数据库管理系统的文件格式,它以表格形式存储数据。在这个主题中,我们将深入探讨如何使用...

    使用Java实现对dbf文件的简单读写

    使用 Java 实现对 dbf 文件的简单读写 Java 是一种广泛使用的编程语言,对于读写 dbf 文件具有重要的应用价值。本文将介绍使用 Java 实现对 dbf 文件的简单读写,包括读写 dbf 文件的基本步骤、相关类的介绍、代码...

    java 实现DBF文件读取与创建

    在Java中处理DBF文件,我们可以借助特定的库,如JDBF,它提供了读取和写入DBF文件的功能。本篇文章将深入探讨如何使用Java实现DBF文件的读取与创建。 1. **DBF文件格式介绍** DBF文件格式源于dBase,是早期个人...

    DBF java 操作 dbf foxpro 例子

    本篇将详细探讨如何使用Java操作DBF文件,以及涉及到的相关知识点。 首先,DBF文件是dBase系列数据库管理系统(包括FoxPro)创建的一种表格文件格式。它包含了表的结构定义和实际数据,支持字段类型如字符、数字、...

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

    总结来说,JavaDBF是一个用于Java平台的DBF文件操作库,提供读取和写入功能。它使得开发者能够方便地在Java应用程序中与DBF文件交互,从而处理那些仍然依赖这种格式的数据。在实际项目中,正确使用JavaDBF可以提高...

    java快速导出几十万百万生成DBF文件数据后台内附有javadbf.jar

    这个库使得Java开发者能够方便地操作DBF文件,而无需了解底层的文件格式细节。使用这个库,你可以创建新的DBF文件,向文件中添加记录,读取现有文件的数据,甚至修改和删除记录。 在处理大规模数据时,性能是个关键...

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

    这种方式利用了Java JDBC技术,通过安装特定的驱动程序,可以将DBF文件视为一个数据库表,并使用标准SQL语句对其进行查询和操作。这种方法的优势在于可以利用现有的SQL技能进行数据处理,同时也支持更复杂的查询和...

    dbf-jdbc-wisecoders,JAVA 读写DBF文件工具包

    1. **JDBC接口**:`dbf-jdbc-wisecoders`通过提供一个类似于JDBC(Java Database Connectivity)的接口,让Java开发者可以使用他们熟悉的SQL查询来访问和操作DBF文件。这大大简化了代码编写,使得DBF文件的处理如同...

    java 读写 DBF 文件 xBaseJ

    总结来说,xBaseJ是Java开发人员处理DBF文件的强大工具,它简化了读写过程,使得在Java应用程序中集成对DBF文件的操作变得轻松易行。如果你需要在项目中处理DBF数据,xBaseJ是一个值得考虑的优秀选择。

    JAVA 读取dbf文件

    这些库提供了API来方便地操作DBF文件。 2. **JDBC-ODBC桥接**: Java的JDBC(Java Database Connectivity)提供了通过ODBC(Open Database Connectivity)接口访问各种数据库的机制。虽然不是直接针对DBF,但可以...

    Java读取DBF文件jar包和测试用例

    这个jar包名为"javadbf-0.4.0.jar",这可能是一个第三方库,它提供了Java API来操作DBF文件。 DBFReader是这个库中的核心组件,可能是一个类或者接口,用于读取DBF文件的内容。通常,DBFReader会提供打开DBF文件、...

    解决了DBF数据和表头乱码的javadbf.jar

    "dbf java"标签表明这是一个关于Java处理DBF文件的专题,对于那些需要在Java应用中处理DBF数据的开发者来说,这个修复后的javadbf.jar及其使用说明是一个宝贵的资源。通过合理使用,可以避免因编码问题导致的数据...

    java快速导出几十万百万生成DBF文件数据后台

    本项目“java快速导出几十万百万生成DBF文件数据后台”主要关注如何使用Java编程语言高效地处理大规模数据,将其导出为DBF文件格式。 首先,我们需要了解Java处理大量数据的基本策略。在Java中,处理大数据的关键...

    java操作dbf+java读取dbf 项目源码

    Java操作DBF,通常指的是在Java程序中处理DBF文件,这是一种常见的数据库文件格式,主要由FoxPro、Visual FoxPro等数据库系统创建。DBF文件主要用于存储结构化的数据,包括表格、字段和记录。在Java中,我们可以通过...

    DBF.rar_DBF_dbf java_java dbf_读取dbf

    在Java中,没有内置的库直接支持DBF文件处理,但有第三方库如JDBF可以方便地进行读取操作。JDBF是一个轻量级的Java库,专门用于处理DBF文件。 3. 使用JDBF读取DBF步骤: a) 引入依赖:首先,你需要在项目中引入...

    java代码DBF转Text

    `javadbf.jar`是一个Java库,专门用于读取和操作DBF文件。在Java中,由于标准库并不直接支持DBF格式,所以需要依赖这样的第三方库来扩展功能。在实际应用中,我们首先需要将`javadbf.jar`添加到项目的类路径中,确保...

Global site tag (gtag.js) - Google Analytics