已阅读5页,还剩15页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
使用Java实现对dbf文件的简单读写将dbf文件的读写基本分成四个类,Writer,Reader, Field, Exception,内容如下,如果需要其Doc文档和完整源码包,请发mail至/* * Title: java访问DBF文件的接口 * Description: 这个类用于表示DBF文件中的写操作 * Copyright: Copyright (c) 2004 * Company: ict * author : He Xiong * version 1.0 */package com.hexiong.jdbf;import java.io.*;import java.util.Calendar;/ Referenced classes of package com.hexiong.jdbf:/ JDBFException, JDBFieldpublic class DBFWriter public DBFWriter(String s, JDBField ajdbfield) throws JDBFException stream = null; recCount = 0; fields = null; fileName = null; dbfEncoding = null; fileName = s; try init(new FileOutputStream(s), ajdbfield); catch(FileNotFoundException filenotfoundexception) throw new JDBFException(filenotfoundexception); public DBFWriter(OutputStream outputstream, JDBField ajdbfield) throws JDBFException stream = null; recCount = 0; fields = null; fileName = null; dbfEncoding = null; init(outputstream, ajdbfield); public DBFWriter(String s, JDBField ajdbfield, String s1) throws JDBFException stream = null; recCount = 0; fields = null; fileName = null; dbfEncoding = null; fileName = s; try dbfEncoding = s1; init(new FileOutputStream(s), ajdbfield); catch(FileNotFoundException filenotfoundexception) throw new JDBFException(filenotfoundexception); private void init(OutputStream outputstream, JDBField ajdbfield) throws JDBFException fields = ajdbfield; try stream = new BufferedOutputStream(outputstream); writeHeader(); for(int i = 0; i ajdbfield.length; i+) writeFieldHeader(ajdbfieldi); stream.write(13); stream.flush(); catch(Exception exception) throw new JDBFException(exception); private void writeHeader() throws IOException byte abyte0 = new byte16; abyte00 = 3; Calendar calendar = Calendar.getInstance(); abyte01 = (byte)(calendar.get(1) - 1900); abyte02 = (byte)calendar.get(2); abyte03 = (byte)calendar.get(5); abyte04 = 0; abyte05 = 0; abyte06 = 0; abyte07 = 0; int i = (fields.length + 1) * 32 + 1; abyte08 = (byte)(i % 256); abyte09 = (byte)(i / 256); int j = 1; for(int k = 0; k fields.length; k+) j += fieldsk.getLength(); abyte010 = (byte)(j % 256); abyte011 = (byte)(j / 256); abyte012 = 0; abyte013 = 0; abyte014 = 0; abyte015 = 0; stream.write(abyte0, 0, abyte0.length); for(int l = 0; l 10) i = 10; for(int j = 0; j i; j+) abyte0j = (byte)s.charAt(j); for(int k = i; k = 10; k+) abyte0k = 0; abyte011 = (byte)jdbfield.getType(); abyte012 = 0; abyte013 = 0; abyte014 = 0; abyte015 = 0; stream.write(abyte0, 0, abyte0.length); for(int l = 0; l 16; l+) abyte0l = 0; abyte00 = (byte)jdbfield.getLength(); abyte01 = (byte)jdbfield.getDecimalCount(); stream.write(abyte0, 0, abyte0.length); public void addRecord(Object aobj) throws JDBFException if(aobj.length != fields.length) throw new JDBFException(Error adding record: Wrong number of values. Expected + fields.length + , got + aobj.length + .); int i = 0; for(int j = 0; j fields.length; j+) i += fieldsj.getLength(); byte abyte0 = new bytei; int k = 0; for(int l = 0; l fields.length; l+) String s = fieldsl.format(aobjl); byte abyte1; try if(dbfEncoding != null) abyte1 = s.getBytes(dbfEncoding); else abyte1 = s.getBytes(); catch(UnsupportedEncodingException unsupportedencodingexception) throw new JDBFException(unsupportedencodingexception); for(int i1 = 0; i1 fieldsl.getLength(); i1+) abyte0k + i1 = abyte1i1; k += fieldsl.getLength(); try stream.write(32); stream.write(abyte0, 0, abyte0.length); stream.flush(); catch(IOException ioexception) throw new JDBFException(ioexception); recCount+; public void close() throws JDBFException try stream.write(26); stream.close(); RandomAccessFile randomaccessfile = new RandomAccessFile(fileName, rw); randomaccessfile.seek(4L); byte abyte0 = new byte4; abyte00 = (byte)(recCount % 256); abyte01 = (byte)(recCount / 256) % 256); abyte02 = (byte)(recCount / 0x10000) % 256); abyte03 = (byte)(recCount / 0x1000000) % 256); randomaccessfile.write(abyte0, 0, abyte0.length); randomaccessfile.close(); catch(IOException ioexception) throw new JDBFException(ioexception); private BufferedOutputStream stream; private int recCount; private JDBField fields; private String fileName; private String dbfEncoding;/* * Title: java访问DBF文件的接口 * Description: 这个类用于表示DBF文件中的字段 * Copyright: Copyright (c) 2004 * Company: ict * author : He Xiong * version 1.0 */package com.hexiong.jdbf;import java.text.*;import java.util.Date;/ Referenced classes of package com.hexiong.jdbf:/ JDBFExceptionpublic class JDBField public JDBField(String s, char c, int i, int j) throws JDBFException if(s.length() 10) throw new JDBFException(The field name is more than 10 characters long: + s); if(c != C & c != N & c != L & c != D & c != F) throw new JDBFException(The field type is not a valid. Got: + c); if(i = 254) throw new JDBFException(The field length should be less than 254 characters for character fields. Got: + i); if(c = N & i = 21) throw new JDBFException(The field length should be less than 21 digits for numeric fields. Got: + i); if(c = L & i != 1) throw new JDBFException(The field length should be 1 characater for logical fields. Got: + i); if(c = D & i != 8) throw new JDBFException(The field length should be 8 characaters for date fields. Got: + i); if(c = F & i = 21) throw new JDBFException(The field length should be less than 21 digits for floating point fields. Got: + i); if(j i - 1) throw new JDBFException(The field decimal count should be less than the length - 1. Got: + j); else name = s; type = c; length = i; decimalCount = j; return; public String getName() return name; public char getType() return type; public int getLength() return length; public int getDecimalCount() return decimalCount; public String format(Object obj) throws JDBFException if(type = N | type = F) if(obj = null) obj = new Double(0.0D); if(obj instanceof Number) Number number = (Number)obj; StringBuffer stringbuffer = new StringBuffer(getLength(); for(int i = 0; i 0) stringbuffer.setCharAt(getLength() - getDecimalCount() - 1, .); DecimalFormat decimalformat = new DecimalFormat(stringbuffer.toString(); String s1 = decimalformat.format(number); int k = getLength() - s1.length(); if(k 0) throw new JDBFException(Value + number + cannot fit in pattern: + stringbuffer + .); StringBuffer stringbuffer2 = new StringBuffer(k); for(int l = 0; l getLength() throw new JDBFException( + obj + is longer than + getLength() + characters.); StringBuffer stringbuffer1 = new StringBuffer(getLength() - s.length(); for(int j = 0; j getLength() - s.length(); j+) stringbuffer1.append( ); return s + stringbuffer1; else throw new JDBFException(Expected a String, got + obj.getClass() + .); if(type = L) if(obj = null) obj = new Boolean(false); if(obj instanceof Boolean) Boolean boolean1 = (Boolean)obj; return boolean1.booleanValue() ? Y : N; else throw new JDBFException(Expected a Boolean, got + obj.getClass() + .); if(type = D) if(obj = null) obj = new Date(); if(obj instanceof Date) Date date = (Date)obj; SimpleDateFormat simpledateformat = new SimpleDateFormat(yyyyMMdd); return simpledateformat.format(date); else throw new JDBFException(Expected a Date, got + obj.getClass() + .); else throw new JDBFException(Unrecognized JDBFField type: + type); public Object parse(String s) throws JDBFException s = s.trim(); if(type = N | type = F) if(s.equals() s = 0; try if(getDecimalCount() = 0) return new Long(s); else return new Double(s); catch(NumberFormatException numberformatexception) throw new JDBFException(numberformatexception); if(type = C) return s; if(type = L) if(s.equals(Y) | s.equals(y) | s.equals(T) | s.equals(t) return new Boolean(true); if(s.equals(N) | s.equals(n) | s.equals(F) | s.equals(f) return new Boolean(false); else throw new JDBFException(Unrecognized value for logical field: + s); if(type = D) SimpleDateFormat simpledateformat = new SimpleDateFormat(yyyyMMdd); try if(.equals(s) return null; else return simpledateformat.parse(s); catch(ParseException parseexception) throw new JDBFException(parseexception); else throw new JDBFException(Unrecognized JDBFField type: + type); public String toString() return name; private String name; private char type; private int length; private int decimalCount;/* * Title: java访问DBF文件的接口 * Description: 这个类用于表示DBF文件中的读写异常 * Copyright: Copyright (c) 2004 * Company: ict * author : He Xiong * version 1.0 */package com.hexiong.jdbf;import java.io.PrintStream;import java.io.PrintWriter;public class JDBFException extends Exception public JDBFException(String s) this(s, null); public JDBFException(Throwable throwable) this(throwable.getMessage(), throwable); public JDBFException(String s, Throwable throwable) super(s); detail = throwable; public String getMessage() if(detail = null) return super.getMessage(); else return super.getMessage(); public void printStackTrace(PrintStream printstream) if(detail = null) super.printStackTrace(printstream); return; PrintStream printstream1 = printstream; printstream1.println(this); detail.printStackTrace(printstream); return; public void printStackTrace() printStackTrace(System.err); public void printStackTrace(PrintWriter printwriter) if(detail = null) super.printStackTrace(printwriter); return; PrintWriter printwriter1 = printwriter; printwriter1.println(this); detail.printStackTrace(printwriter); return; private Throwable detail;/* * Title: java访问DBF文件的接口 * Description: 这个类用于表示DBF文件中的读操作 * Copyright: Copyright (c) 2004 * Company: ict * author : He Xiong * version 1.0 */package com.hexiong.jdbf;import java.io.*;/ Referenced classes of package com.hexiong.jdbf:/ JDBFException, JDBFieldpublic class DBFReader public DBFReader(String s) throws JDBFException stream = null; fields = null; nextRecord = null; try init(new FileInputStream(s); catch(FileNotFoundException filenotfoundexception) throw new JDBFException(filenotfoundexception); public DBFReader(InputStream inputstream) throws JDBFException stream = null; fields = null; nextRecord = null; init(inputstream); private void init(InputStream inputstream) throws JDBFException try stream = new DataInputStream(inputstream); int i = readHeader(); fields = new JDBFieldi; int j = 1; for(int k = 0; k i; k+) fieldsk = readFieldHeader(); j += fieldsk.getLength(); if(stream.read() 1) throw new JDBFException(Unexpected end of file reached.); nextRecord = new bytej; try stream.readFully(nextRecord); catch(EOFException eofexception) nextRecord = null; stream.close(); catch(IOException ioexception) throw new JDBFException(ioexception); private int readHeader() throws IOException, JDBFException byte abyte0 = new byte16; try stream.readFully(abyte0); catch(EOFException eofexception) throw new JDBFException(Unexpected end of file reached.); int i = abyte08; if(i 0) i += 256; i += 256 * abyte09; i = -i / 32; i-; try stream.readFully(abyte0);
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 门诊护理继续教育途径
- 重度子痫前期的护理伦理与法律问题
- 重症监护患者的康复护理策略
- 重症胰腺炎的感染控制与护理
- 大血管术后预防感染护理措施
- 戒毒矫治技术考研试题及答案
- 室间隔缺损经导管封堵治疗
- 橡胶成型工岗位综合应用考核试卷含答案
- 浮选工岗前质量控制考核试卷含答案
- 生活垃圾焚烧操作工风险评估与管理竞赛考核试卷含答案
- 做账实操-财务交接及半路建账实操SOP
- 未成年人家庭监护能力评估通知书、参考指标、评估报告(参考)
- 学校结构化面试试题及答案
- 2025年江苏省苏州市工业园区事业单位招聘考试综合类专业能力测试试卷及答案
- 2026中邮人寿保险股份有限公司校园招聘备考考试题库附答案解析
- 2025年中国花岗岩石材数据监测报告
- 人工智能应用技术基础 课件 项目七 解码人工智能生成内容AIGC的独特技术
- 培智洗衣服课件
- 医疗纠纷预防和处理课件
- 2025年甘肃省中考英语试卷真题(含标准答案及解析)
- 护士呼吸科进修专题汇报
评论
0/150
提交评论