1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import com.linuxense.javadbf.DBFException;
import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFReader;
public class JavaDBFReaderTest {
public static void main( String args[]) {
try {
// create a DBFReader object
//
InputStream inputStream = new FileInputStream( args[ 0]); // take dbf file as program argument
DBFReader reader = new DBFReader( inputStream);
// get the field count if you want for some reasons like the following
//
int numberOfFields = reader.getFieldCount();//nb de champ d'une table
// use this count to fetch all field information
// if required
//
String Colonne ="";
for( int i=0; i<numberOfFields; i++) {
DBFField field = reader.getField( i);//on recupere les noms des colonnes
// do something with it if you want
// refer the JavaDoc API reference for more details
//
Colonne = Colonne+";"+field.getName();
// System.out.println( field.getName());
}
System.out.println("nb de colonnes: "+numberOfFields);
// System.out.println( Colonne);
// Now, lets us start reading the rows
//
Object []rowObjects;
String Ligne = "";
int nbDeLigne=0;
int nbDeLigneTotale = reader.getRecordCount();
while( (rowObjects = reader.nextRecord()) != null) {
nbDeLigne++;
for( int i=0; i<rowObjects.length; i++) {
Ligne=Ligne+";"+rowObjects[i];
// System.out.println( rowObjects[i]);
}
//System.out.println(Ligne);
System.out.println("Enregistrement "+nbDeLigne+" / "+nbDeLigneTotale);
}
// By now, we have itereated through all of the rows
System.out.println("nb de ligne découverte: "+nbDeLigne);
inputStream.close();
}
catch( DBFException e) {
System.out.println( e.getMessage());
}
catch( IOException e) {
System.out.println( e.getMessage());
}
}
} |
Partager