/* */ package com.linuxense.javadbf;
/* */
/* */ import java.io.DataOutput;
/* */ import java.io.DataOutputStream;
/* */ import java.io.File;
/* */ import java.io.FileNotFoundException;
/* */ import java.io.IOException;
/* */ import java.io.OutputStream;
/* */ import java.io.RandomAccessFile;
/* */ import java.util.Date;
/* */ import java.util.GregorianCalendar;
/* */ import java.util.Vector;
/* */
/* */ public class DBFWriter extends DBFBase
/* */ {
private String jdField_characterSetName_of_type_JavaLangString;
/* */ DBFHeader header;
/* 30 */ Vector v_records = new Vector();
/* 31 */ int recordCount = 0;
/* 32 */ RandomAccessFile raf = null;
/* 33 */ boolean appendMode = false;
/* */
/* */ public DBFWriter()
/* */ {
/* 40 */ this.header = new DBFHeader();
/* */ }
/* */
/* */ public DBFWriter(File dbfFile)
/* */ throws DBFException
/* */ {
/* */ try
/* */ {
/* 53 */ this.raf = new RandomAccessFile(dbfFile, "rw");
/* */
/* 58 */ if ((!dbfFile.exists()) || (dbfFile.length() == 0L))
/* */ {
/* 60 */ this.header = new DBFHeader();
/* 61 */ return;
/* */ }
/* */
/* 64 */ this.header = new DBFHeader();
/* 65 */ this.header.read(this.raf);
/* */
/* 68 */ this.raf.seek(this.raf.length() - 1L);
/* */ }
/* */ catch (FileNotFoundException e)
/* */ {
/* 72 */ throw new DBFException("Specified file is not found. " + e.getMessage());
/* */ }
/* */ catch (IOException e)
/* */ {
/* 76 */ throw new DBFException(e.getMessage() + " while reading header");
/* */ }
/* */
/* 79 */ this.recordCount = this.header.numberOfRecords;
/* */ }
/* */
/* */ public void setFields(DBFField[] fields)
/* */ throws DBFException
/* */ {
/* 88 */ if (this.header.fieldArray != null)
/* */ {
/* 90 */ throw new DBFException("Fields has already been set");
/* */ }
/* */
/* 93 */ if ((fields == null) || (fields.length == 0))
/* */ {
/* 95 */ throw new DBFException("Should have at least one field");
/* */ }
/* */
/* 98 */ for (int i = 0; i < fields.length; i++)
/* */ {
/* 100 */ if (fields[i] == null)
/* */ {
/* 102 */ throw new DBFException("Field " + (i + 1) + " is null");
/* */ }
/* */ }
/* */
/* 106 */ this.header.fieldArray = fields;
/* */ try
/* */ {
/* 110 */ if ((this.raf != null) && (this.raf.length() == 0L))
/* */ {
/* 115 */ this.header.write(this.raf);
/* */ }
/* */ }
/* */ catch (IOException e)
/* */ {
/* 120 */ throw new DBFException("Error accesing file");
/* */ }
/* */ }
/* */
/* */ public void addRecord(Object[] values)
/* */ throws DBFException
/* */ {
/* 130 */ if (this.header.fieldArray == null)
/* */ {
/* 132 */ throw new DBFException("Fields should be set before adding records");
/* */ }
/* */
/* 135 */ if (values == null)
/* */ {
/* 137 */ throw new DBFException("Null cannot be added as row");
/* */ }
/* */
/* 140 */ if (values.length != this.header.fieldArray.length)
/* */ {
/* 142 */ throw new DBFException("Invalid record. Invalid number of fields in row");
/* */ }
/* */
/* 145 */ for (int i = 0; i < this.header.fieldArray.length; i++)
/* */ {
/* 147 */ if (values[i] != null)
/* */ {
/* 152 */ switch (this.header.fieldArray[i].getDataType())
/* */ {
/* */ case 67:
/* 155 */ if (!(values[i] instanceof String)) {
/* 156 */ throw new DBFException("Invalid value for field " + i);
/* */ }
/* */
/* */ break;
/* */ case 76:
/* 161 */ if (!(values[i] instanceof Boolean)) {
/* 162 */ throw new DBFException("Invalid value for field " + i);
/* */ }
/* */
/* */ break;
/* */ case 78:
/* 167 */ if (!(values[i] instanceof Double)) {
/* 168 */ throw new DBFException("Invalid value for field " + i);
/* */ }
/* */
/* */ break;
/* */ case 68:
/* 173 */ if (!(values[i] instanceof Date)) {
/* 174 */ throw new DBFException("Invalid value for field " + i);
/* */ }
/* */
/* */ break;
/* */ case 70:
/* 179 */ if (!(values[i] instanceof Double))
/* */ {
/* 181 */ throw new DBFException("Invalid value for field " + i); } break;
/* */ case 69:
/* */ case 71:
/* */ case 72:
/* */ case 73:
/* */ case 74:
/* */ case 75:
/* 187 */ case 77: } } } if (this.raf == null)
/* */ {
/* 189 */ this.v_records.addElement(values);
/* */ }
/* */ else
/* */ {
/* */ try
/* */ {
/* 195 */ writeRecord(this.raf, values);
/* 196 */ this.recordCount += 1;
/* */ }
/* */ catch (IOException e)
/* */ {
/* 200 */ throw new DBFException("Error occured while writing record. " + e.getMessage());
/* */ }
/* */ }
/* */ }
/* */
/* */ public void write(OutputStream out)
/* */ throws DBFException
/* */ {
/* */ try
/* */ {
/* 213 */ if (this.raf == null)
/* */ {
/* 215 */ DataOutputStream outStream = new DataOutputStream(out);
/* */
/* 217 */ this.header.numberOfRecords = this.v_records.size();
/* 218 */ this.header.write(outStream);
/* */
/* 221 */ int t_recCount = this.v_records.size();
/* 222 */ for (int i = 0; i < t_recCount; i++)
/* */ {
/* 224 */ Object[] t_values = (Object[])this.v_records.elementAt(i);
/* */
/* 226 */ writeRecord(outStream, t_values);
/* */ }
/* */
/* 229 */ outStream.write(26);
/* 230 */ outStream.flush();
/* */ }
/* */ else
/* */ {
/* 235 */ this.header.numberOfRecords = this.recordCount;
/* 236 */ this.raf.seek(0L);
/* 237 */ this.header.write(this.raf);
/* 238 */ this.raf.seek(this.raf.length());
/* 239 */ this.raf.writeByte(26);
/* 240 */ this.raf.close();
/* */ }
/* */
/* */ }
/* */ catch (IOException e)
/* */ {
/* 246 */ throw new DBFException(e.getMessage());
/* */ }
/* */ }
/* */
/* */ public void write()
/* */ throws DBFException
/* */ {
/* 253 */ write(null);
/* */ }
/* */
/* */ private void writeRecord(DataOutput dataOutput, Object[] objectArray)
/* */ throws IOException
/* */ {
/* 259 */ dataOutput.write(32);
/* 260 */ for (int j = 0; j < this.header.fieldArray.length; j++)
/* */ {
/* 262 */ switch (this.header.fieldArray[j].getDataType())
/* */ {
/* */ case 67:
/* 265 */ if (objectArray[j] != null)
/* */ {
/* 267 */ String str_value = objectArray[j].toString();
/* 268 */ dataOutput.write(Utils.textPadding(str_value, this.jdField_characterSetName_of_type_JavaLangString, this.header.fieldArray[j].getFieldLength()));
/* */ }
/* */ else
/*
java dbf reader
需积分: 49 164 浏览量
2015-04-20
22:46:16
上传
评论 2
收藏 10KB RAR 举报
lwbkfc
- 粉丝: 2
- 资源: 5
最新资源
- 农村信用社联合社计算机信息系统投产与变更管理办.docx
- 农村信用社联合社计算机信息系统数据管理办法.docx
- 利用SPSS作临床效度分析线上计算网站介绍-医学研究部统计谘.(医学PPT课件).ppt
- 利用Zabbix监控mysqldump定时备份数据库状态.docx
- 利用计算机解决问题的基本过程.doc
- 化工铁路通信工程总结.doc
- 北京大学网络教育软件工程作业.docx
- 医药公司(连锁店)计算机操作规程未新系统的自行按照旧制修改-新系统过制的编号加修模版.doc
- 医药公司(连锁店)计算机系统操作规程模版.doc
- 医药连锁门店计算机系统的操作和管理程序未新系统的自行按照旧制修改-新系统过制的编号加修模版.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈