Simple reading and writing to DBF files using Java

zhaozj2021-02-16  77

I brought the reading and writing of the DBF file into four classes, Writer, Reader, Field, Exception, the content is as follows, if there is any problem or good suggestion when using the package, please send mail to iiihero #hotmail.com Download For details, please refer to: http://computer.mblogger.cn/EHERO/POSTS/31204.aspx If you can reor, I will be more happy.

/ ** *

Title: Java Access the interface of the DBF file *

Description: This class is used to represent the write operation in the DBF file *

Copyright: CopyRight (C) 2004 *

Company: ICT * @Author: He Xiong * @version 1.0 * /

Package com.hexiong.jdbf;

Import java.io. *; import java.util.calendar;

// Reference Classes of package com.hexiong.jdbf: // jdbfexception, jdbfield

Public 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

Stream.write (13); stream.flush ();} catch (exception exception) {throw new jdbfexception;}}

Private void writeHeader () throws ioException {byte abyte0 [] = new byte [16]; abyte0 [0] = 3; Calendar Calendar = 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; INT I = (Fields.Length 1) * 32 1; Abyte0 [8] = (Byte) (I% 256); Abyte0 [9] = (Byte) I / 256); int J = 1; for (int K = 0; K

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 (int L = 0; l <16; l ) ABYTE0 [L] = 0; stream.write (Abyte0, 0, Abyte0.Length }

Private void writefieldHeader (jdbfield jdbfield) throws ioException {byte abyte0 [] = new byte [16]; string s = jdbfield.getname (); int i = s.Length (); if (i> 10) i = 10; for (int J = 0; j

For (int K = 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 (int L = 0; l <16; l ) ABYTE0 [L] = 0;

Abyte0 [0] = (byte) jdbfield.getLength (); Abyte0 [1] = (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

BYTE ABYTE0 [] = new byte [i]; int K = 0; for (int L = 0; l

Try {stream.write (32); stream.write (abyte0, 0, abyte0.LEngth); stream.flush ();} catch (ioException oException) {throw new jdbfexception (ooException);} Reccount ;}

public void close () throws JDBFException {try {stream.write (26); stream.close (); RandomAccessFile randomaccessfile = new RandomAccessFile (fileName, "rw"); randomaccessfile.seek (4L); byte abyte0 [] = new byte [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 (} catch (} catch ({ooException ioException) {throw new JDBFEXCEPTION (IOException);}} private bufferedoutputStream Stream; private intect; private jdbfield fields []; private string filename; private string dbfencoding;}

/ ** *

Title: Java Access the interface of the DBF file *

Description: This class is used to represent fields in the DBF file *

Copyright: CopyRight (C) 2004 *

Company: ICT * @Author: He xiong * @version 1.0 * /

Package com.hexiong.jdbf;

Import java.text. *; import java.util.date;

// Reference Classes of Package Com.hexiong.jdbf: // JDBFEXCEPTION

Public 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 <1) throw new jdbfexception (" The Field Length Should Be a Positive Integer. Got: " i); if (c == 'C' && 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 cha Racaters 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 <0) throw new JDBFEXCEPTION ("The Field Decimal Count Should Not Be a NEGATIVE INTEGER. Got:" J); IF ((c == 'c' || c == 'L' || c =

= 'D') &&j! = 0) Throw new JDBFEXCEPTION ("The Field Decimal Count Should Be 0 for Character, Logical, And Date Fields. Got:" J); 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 () {returni

Public int getLength () {return longth;}

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

if (getDecimalCount ()> 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

RETURN S STRINGBUFFER1;} else {throw new JDBFEXCEPTION ("Expected A String, Got" ");}}} (type == 'L') {IF (Obj == Null) Obj = new boolean (false) {boolean boolean1 = (boolean) obj; returnolean1.booleanValue ()? "y": "n";} else {throw new jdbfexception ("Expected A Boolean, Got " Obj.getClass () ");}} f (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);}} (type == 'd') {s impleDateFormat 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 int lay; private int decimalcount;}

/ ** *

Title: Java Access the interface of the DBF file *

Description: This class is used to represent the reading and writing exception in the DBF file *

Copyright: CopyRight (C ) 2004 *

Company: ICT * @Author: he xiong * @version 1.0 * /

Package com.hexiong.jdbf;

Imports java.io.printStream; import java.io.printwriter;

Public class jdbfexception extends exception {

Public JDBFEXCEPTION (STRING S) {THIS (S, NULL);}

Public JDBFexception (thiwable throwable) {this (thiwable.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; }pritwriter printwriter1 = printwriter;

PrintWriter1.Println (this); detail.printstacktrace (printwriter); return;

PRIVATE throwable detail;}

/ ** *

Title: Java Access the interface of the DBF file *

Description: This class is used to represent read operations in the DBF file *

Copyright: CopyRight (C) 2004 *

Company: ICT * @Author: He xiong * @version 1.0 * / package com.hexiong.jdbf;

Import java.io. *;

// Reference Classes of package com.hexiong.jdbf: // jdbfexception, jdbfield

Public 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 JDBField [i]; int j = 1; for (int k = 0; k

if (stream.read () <1) throw new JDBFException ( "Unexpected end of file reached."); nextRecord = new byte [j]; 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 byte [16]; try {stream. ReadFully (ABYTE0);} Catch ("Unexpected End Ofbfexception");} INT i = Abyte0 [8]; IF (i <0) i = 256; i = 256 * Abyte0 [9]; i = --i / 32; i-; try {stream.readfully (abyte0);} catch (EOFEXCEPTION EOFEXC "undexpected end});} return i;

private JDBField readFieldHeader () throws IOException, JDBFException {byte abyte0 [] = new byte [16]; try {stream.readFully (abyte0);} catch (EOFException eofexception) {throw new JDBFException ( "Unexpected end of file reached.") Stringbuffer stringbuffer = new stringbuffer (10); for (int i = 0; i <10; i ) {if (Abyte0 [i] == 0) Break; stringbuffer.append ((char) Abyte0 [i]); }

CHAR C = (char) ABYTE0 [11]; try {stream.readfully (abyte0);} catch (eofexception eofexception1) {throw new jdbfexception ("Unexpected End of File Reached.");} int J = Abyte0 [0]; INT K = ABYTE0 [1]; if (j <0) j = 256; if (k <0) k = 256; return new jdbfield (StringBuffer.toString (), C, J, K);}

Public int getfieldcount () {return fields.length;}

Public JDBFIELD GETFIELD (INT i) {return fields [i];

Public Boolean HasnexTrecord () {Return NEXTRECORD! = NULL;

public Object [] nextRecord () throws JDBFException {if throw new JDBFException (hasNextRecord ()!) ( "No more records available."); Object aobj [] = new Object [fields.length]; int i = 1; for ( INT J = 0; j

try {stream.readFully (nextRecord);} catch (EOFException eofexception) {nextRecord = null;} catch (IOException ioexception) {throw new JDBFException (ioexception);} return aobj;}

public void close () throws JDBFException {nextRecord = null; try {stream.close ();} catch (IOException ioexception) {throw new JDBFException (ioexception);}} private DataInputStream stream; private JDBField fields []; private byte nextRecord [ }

Finally, give a simple test sample program

/ ** *

Title: Java Access the interface of the DBF file *

Description: Test the reading and writing of the DBF file *

Copyright: Copyright (c) 2004 *

Company: ICT * @Author: he xiong * @version 1.0 * /

Import com.hexiong.jdbf.dbfreader; import java.io.printStream; import java.net.URL;

Public class test {

Public test () {}

Public static void main (string args []) throws exception {dbfreader {dbfreader = new dbfreader ("e: //lakes.dbf"); int i; for (i = 0; dbfreader.hasnexTrecord (); i ) {Object Aobj [] = dbfreader.nexTrecord (); for (int J = 0; j

System.out.println ("Total Count:" i);}}

转载请注明原文地址:https://www.9cbs.com/read-21790.html

New Post(0)