Recently to do a program, the data source is DBF file, I use Java to parse it, first need to download a Java dedicated jar package javadbf-0.4.0.jar, this is the latest version. The parsing code is as follows:
Public static void main (string [] ARGs ){
Try {
Object [] OBJ = NULL;
// Create a file stream
Inputstream is = new fileinputstream ("Q. DBF ");
// Initialize the dbfreader instance
Dbfreader df = new dbfreader (is );
// Set the encoding format. If the parsed data is garbled, you can add this sentence.
DF. setcharactersetname ("gb2312 ");
// Obtain the number of file Fields
Int filecount = DF. getfieldcount ();
// Retrieve the field information
For (INT I = 0; I <filecount; I ++ ){
Dbffield field = DF. getfield (I );
// System. Out. Print (field. getname () + "/t ");
}
System. Out. println ();
Int I = 0;
While (OBJ = DF. nextrecord ())! = NULL) {// when parsing some DBF Files, an error is reported in this sentence, that is, an error occurs when reading data into an object array.
For (Int J = 0; j <obj. length; j ++ ){
System. Out. Print (OBJ [J] + "/t ");
// System. Out. println (OBJ [1]);
}
If (I ++> 20) break;
System. Out. println ();
}
Is. Close ();
} Catch (exception ex) {ex. printstacktrace ();}
}
Error 1 found:
While (OBJ = DF. nextrecord ())! = NULL) {// when parsing some DBF Files, an error is reported in this sentence, that is, an error occurs when reading data into an object array.
Error: failed to parse number: for input string: "-. ---". After carefully searching and viewing the DBF source file, it is found that the DBF file contains special characters that cannot be parsed. Not with the case, find the javadbf-0.4.0.jar source code. After debugging, we found the following in the dbfreader class:
Case 'N ':
Try {
Byte t_numeric [] = new byte [This. header. fieldarray [I]. getfieldlength ()];
Datainputstream. Read (t_numeric );
T_numeric = utils. trimleftspaces (t_numeric );
String strtemp = new string (t_numeric );
If (strtemp. Equals ("-. ---") | strtemp. Equals ("-")){
Recordobjects [I] = 0.0; // This line of code does not need to be added when you only need to output the data. When you want to read the data above, you must add it.
Continue;
}
// The code I added here is to block this error.
If (t_numeric.length> 0 &&! Utils. Contains (t_numeric, (byte )'? ')){
Recordobjects [I] = new double (new string (t_numeric ));
}
Else {
Recordobjects [I] = NULL;
}
}
Error 2:
It is the specification in the DBF file. The 0x20 mark indicates that it is not deleted, and the 0 x 2a mark indicates that it is deleted. However, the data in it is still normal, and updates will be updated, but we cannot read the data with the 0x2a mark during reading. All of them have to be processed here. After debugging the source code, you can modify it in one place.
Boolean isdeleted = false;
Do {
If (isdeleted ){
Datainputstream. Skip (this. header. recordLength-1 );
}
Int t_byte = datainputstream. readbyte ();
If (t_byte = end_of_data ){
Return NULL;
}
// Isdeleted = (t_byte = '*');
// This sentence is original
//Isdeleted = false; // This is modified. After the modification, No matter what mark it is, it can be read normally.
} While (isdeleted );
At present, we have found errors in these two cases. Sometimes these two errors will occur at the same time, so sometimes both of them must be modified.
Write this item for your sharing.