使用Java实现对dbf文件的简单读写.docx
- 文档编号:8615537
- 上传时间:2023-02-01
- 格式:DOCX
- 页数:23
- 大小:18.85KB
使用Java实现对dbf文件的简单读写.docx
《使用Java实现对dbf文件的简单读写.docx》由会员分享,可在线阅读,更多相关《使用Java实现对dbf文件的简单读写.docx(23页珍藏版)》请在冰豆网上搜索。
使用Java实现对dbf文件的简单读写
使用Java实现对dbf文件的简单读写
将dbf文件的读写基本分成四个类,Writer,Reader,Field,Exception,内容如下,如果需要其Doc文档和完整源码包,请发mail至iihero@
/**
*
Title:
java访问DBF文件的接口
*
Description:
这个类用于表示DBF文件中的写操作
*
Copyright:
Copyright(c)2004
*
Company:
ict
*@author:
HeXiong
*@version1.0
*/
packagecom.hexiong.jdbf;
importjava.io.*;
importjava.util.Calendar;
//Referencedclassesofpackagecom.hexiong.jdbf:
//JDBFException,JDBField
publicclassDBFWriter
{
publicDBFWriter(Strings,JDBFieldajdbfield[])
throwsJDBFException
{
stream=null;
recCount=0;
fields=null;
fileName=null;
dbfEncoding=null;
fileName=s;
try
{
init(newFileOutputStream(s),ajdbfield);
}
catch(FileNotFoundExceptionfilenotfoundexception)
{
thrownewJDBFException(filenotfoundexception);
}
}
publicDBFWriter(OutputStreamoutputstream,JDBFieldajdbfield[])
throwsJDBFException
{
stream=null;
recCount=0;
fields=null;
fileName=null;
dbfEncoding=null;
init(outputstream,ajdbfield);
}
publicDBFWriter(Strings,JDBFieldajdbfield[],Strings1)
throwsJDBFException
{
stream=null;
recCount=0;
fields=null;
fileName=null;
dbfEncoding=null;
fileName=s;
try
{
dbfEncoding=s1;
init(newFileOutputStream(s),ajdbfield);
}
catch(FileNotFoundExceptionfilenotfoundexception)
{
thrownewJDBFException(filenotfoundexception);
}
}
privatevoidinit(OutputStreamoutputstream,JDBFieldajdbfield[])
throwsJDBFException
{
fields=ajdbfield;
try
{
stream=newBufferedOutputStream(outputstream);
writeHeader();
for(inti=0;i writeFieldHeader(ajdbfield[i]); stream.write(13); stream.flush(); } catch(Exceptionexception) { thrownewJDBFException(exception); } } privatevoidwriteHeader() throwsIOException { byteabyte0[]=newbyte[16]; abyte0[0]=3; Calendarcalendar=Calendar.getInstance(); abyte0[1]=(byte)(calendar.get (1)-1900); abyte0[2]=(byte)calendar.get (2); abyte0[3]=(byte)calendar.get(5); abyte0[4]=0; abyte0[5]=0; abyte0[6]=0; abyte0[7]=0; inti=(fields.length+1)*32+1; abyte0[8]=(byte)(i%256); abyte0[9]=(byte)(i/256); intj=1; for(intk=0;k j+=fields[k].getLength(); abyte0[10]=(byte)(j%256); abyte0[11]=(byte)(j/256); abyte0[12]=0; abyte0[13]=0; abyte0[14]=0; abyte0[15]=0; stream.write(abyte0,0,abyte0.length); for(intl=0;l<16;l++) abyte0[l]=0; stream.write(abyte0,0,abyte0.length); } privatevoidwriteFieldHeader(JDBFieldjdbfield) throwsIOException { byteabyte0[]=newbyte[16]; Strings=jdbfield.getName(); inti=s.length(); if(i>10) i=10; for(intj=0;j abyte0[j]=(byte)s.charAt(j); for(intk=i;k<=10;k++) abyte0[k]=0; abyte0[11]=(byte)jdbfield.getType(); abyte0[12]=0; abyte0[13]=0; abyte0[14]=0; abyte0[15]=0; stream.write(abyte0,0,abyte0.length); for(intl=0;l<16;l++) abyte0[l]=0; abyte0[0]=(byte)jdbfield.getLength(); abyte0[1]=(byte)jdbfield.getDecimalCount(); stream.write(abyte0,0,abyte0.length); } publicvoidaddRecord(Objectaobj[]) throwsJDBFException { if(aobj.length! =fields.length) thrownewJDBFException("Erroraddingrecord: Wrongnumberofvalues.Expected"+fields.length+",got"+aobj.length+"."); inti=0; for(intj=0;j i+=fields[j].getLength(); byteabyte0[]=newbyte[i]; intk=0; for(intl=0;l { Strings=fields[l].format(aobj[l]); byteabyte1[]; try { if(dbfEncoding! =null) abyte1=s.getBytes(dbfEncoding); else abyte1=s.getBytes(); } catch(UnsupportedEncodingExceptionunsupportedencodingexception) { thrownewJDBFException(unsupportedencodingexception); } for(inti1=0;i1 abyte0[k+i1]=abyte1[i1]; k+=fields[l].getLength(); } try { stream.write(32); stream.write(abyte0,0,abyte0.length); stream.flush(); } catch(IOExceptionioexception) { thrownewJDBFException(ioexception); } recCount++; } publicvoidclose() throwsJDBFException { try { stream.write(26); stream.close(); RandomAccessFilerandomaccessfile=newRandomAccessFile(fileName,"rw"); randomaccessfile.seek(4L); byteabyte0[]=newbyte[4]; abyte0[0]=(byte)(recCount%256); abyte0[1]=(byte)((recCount/256)%256); abyte0[2]=(byte)((recCount/0x10000)%256); abyte0[3]=(byte)((recCount/0x1000000)%256); randomaccessfile.write(abyte0,0,abyte0.length); randomaccessfile.close(); } catch(IOExceptionioexception) { thrownewJDBFException(ioexception); } } privateBufferedOutputStreamstream; privateintrecCount; privateJDBFieldfields[]; privateStringfileName; privateStringdbfEncoding; } /** * Title: java访问DBF文件的接口 * Description: 这个类用于表示DBF文件中的字段 * Copyright: Copyright(c)2004 * Company: ict *@author: HeXiong *@version1.0 */ packagecom.hexiong.jdbf; importjava.text.*; importjava.util.Date; //Referencedclassesofpackagecom.hexiong.jdbf: //JDBFException publicclassJDBField { publicJDBField(Strings,charc,inti,intj) throwsJDBFException { if(s.length()>10) thrownewJDBFException("Thefieldnameismorethan10characterslong: "+s); if(c! ='C'&&c! ='N'&&c! ='L'&&c! ='D'&&c! ='F') thrownewJDBFException("Thefieldtypeisnotavalid.Got: "+c); if(i<1) thrownewJDBFException("Thefieldlengthshouldbeapositiveinteger.Got: "+i); if(c=='C'&&i>=254) thrownewJDBFException("Thefieldlengthshouldbelessthan254charactersforcharacterfields.Got: "+i); if(c=='N'&&i>=21) thrownewJDBFException("Thefieldlengthshouldbelessthan21digitsfornumericfields.Got: "+i); if(c=='L'&&i! =1) thrownewJDBFException("Thefieldlengthshouldbe1characaterforlogicalfields.Got: "+i); if(c=='D'&&i! =8) thrownewJDBFException("Thefieldlengthshouldbe8characatersfordatefields.Got: "+i); if(c=='F'&&i>=21) thrownewJDBFException("Thefieldlengthshouldbelessthan21digitsforfloatingpointfields.Got: "+i); if(j<0) thrownewJDBFException("Thefielddecimalcountshouldnotbeanegativeinteger.Got: "+j); if((c=='C'||c=='L'||c=='D')&&j! =0) thrownewJDBFException("Thefielddecimalcountshouldbe0forcharacter,logical,anddatefields.Got: "+j); if(j>i-1) { thrownewJDBFException("Thefielddecimalcountshouldbelessthanthelength-1.Got: "+j); }else { name=s; type=c; length=i; decimalCount=j; return; } } publicStringgetName() { returnname; } publicchargetType() { returntype; } publicintgetLength() { returnlength; } publicintgetDecimalCount() { returndecimalCount; } publicStringformat(Objectobj) throwsJDBFException { if(type=='N'||type=='F') { if(obj==null) obj=newDouble(0.0D); if(objinstanceofNumber) { Numbernumber=(Number)obj; StringBufferstringbuffer=newStringBuffer(getLength()); for(inti=0;i stringbuffer.append("#"); if(getDecimalCount()>0) stringbuffer.setCharAt(getLength()-getDecimalCount()-1,'.'); DecimalFormatdecimalformat=newDecimalFormat(stringbuffer.toString()); Strings1=decimalformat.format(number); intk=getLength()-s1.length(); if(k<0) thrownewJDBFException("Value"+number+"cannotfitinpattern: '"+stringbuffer+"'."); StringBufferstringbuffer2=newStringBuffer(k); for(intl=0;l stringbuffer2.append(""); returnstringbuffer2+s1; }else { thrownewJDBFException("ExpectedaNumber,got"+obj.getClass()+"."); } } if(type=='C') { if(obj==null) obj=""; if(objinstanceofString) { Strings=(String)obj; if(s.length()>getLength()) thrownewJDBFException("'"+obj+"'islongerthan"+getLength()+"characters."); StringBufferstringbuffer1=newStringBuffer(getLength()-s.length()); for(intj=0;j stringbuffer1.append(''); returns+stringbuffer1; }else { thrownewJDBFException("ExpectedaString,got"+obj.getClass()+"."); } } if(type=='L') { if(obj==null) obj=newBoolean(false); if(objinstanceofBoolean) { Booleanboolean1=(Boolean)obj; returnboolean1.booleanValue()? "Y": "N"; }else { thrownewJDBFException("ExpectedaBoolean,got"+obj.getClass()+"."); } } if(type=='D') { if(obj==null) obj=newDate(); if(objinstanceofDate) { Datedate=(Date)obj; SimpleDateFormatsimpledateformat=newSimpleDateFormat("yyyyMMdd"); returnsimpledateformat.format(date); }else { thrownewJDBFException("ExpectedaDate,got"+obj.getClass()+"."); } }else { thrownewJDBFException("UnrecognizedJDBFFieldtype: "+type); } } publicObjectparse(Strings) throwsJDBFException { s=s.trim(); if(type=='N'||type=='F') { if(s.equals("")) s="0"; try { if(getDecimalCount()==0) returnnewLong(s); else returnnewDouble(s); } catch(NumberFormatExceptionnumberformatexception) { thrownewJDBFException(numberformatexception); } } if(type=='C') returns; if(type=='L') { if(s.equals("Y")||s.equals("y")||s.equals("T")||s.equals("t")) returnnewBoolean(true); if(s.equals("N")||s.equals("n")||s.equals("F")||s.equals("f")) returnnewBoolean(false); else thrownewJDBFException("Unrecognizedvalueforlogicalfield: "+s)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 使用 Java 实现 dbf 文件 简单 读写
![提示](https://static.bdocx.com/images/bang_tan.gif)