/* DBFWriter Class for defining a DBF structure and addin data to that structure and finally writing it to an OutputStream. This file is part of JavaDBF packege. author: anil@linuxense.com license: LGPL (http://www.gnu.org/copyleft/lesser.html) $Id: DBFWriter.java,v 1.9 2004/03/31 10:57:16 anil Exp $ */ package com.linuxense.javadbf; import java.io.*; import java.util.*; /** An object of this class can create a DBF file. Create an object,
then define fields by creating DBFField objects and
add them to the DBFWriter object
add records using the addRecord() method and then
call write() method. */ public class DBFWriter extends DBFBase { /* other class variables */ DBFHeader header; Vector v_records = new Vector(); int recordCount = 0; RandomAccessFile raf = null; /* Open and append records to an existing DBF */ boolean appendMode = false; /** Creates an empty Object. */ public DBFWriter() { this.header = new DBFHeader(); } /** Creates a DBFWriter which can append to records to an existing DBF file. @param dbfFile. The file passed in shouls be a valid DBF file. @exception Throws DBFException if the passed in file does exist but not a valid DBF file, or if an IO error occurs. */ public DBFWriter( File dbfFile) throws DBFException { try { this.raf = new RandomAccessFile( dbfFile, "rw"); /* before proceeding check whether the passed in File object is an empty/non-existent file or not. */ if( !dbfFile.exists() || dbfFile.length() == 0) { this.header = new DBFHeader(); return; } header = new DBFHeader(); this.header.read( raf); /* position file pointer at the end of the raf */ this.raf.seek( this.raf.length()-1 /* to ignore the END_OF_DATA byte at EoF */); } catch( FileNotFoundException e) { throw new DBFException( "Specified file is not found. " + e.getMessage()); } catch( IOException e) { throw new DBFException( e.getMessage() + " while reading header"); } this.recordCount = this.header.numberOfRecords; } /** Sets fields. */ public void setFields( DBFField[] fields) throws DBFException { if( this.header.fieldArray != null) { throw new DBFException( "Fields has already been set"); } if( fields == null || fields.length == 0) { throw new DBFException( "Should have at least one field"); } for( int i=0; i