Voici un exemple simple
La copie de code est la suivante:
package com.cramc;
import java.io.fileInputStream;
import java.io.filenotfoundException;
Importer java.io.fileOutputStream;
import java.io.inputStream;
import java.io.outputStream;
import com.linuxense.javadbf.dbfexception;
import com.linuxense.javadbf.dbffield;
import com.linuxense.javadbf.dbfreader;
import com.linuxense.javadbf.dbfwriter;
classe publique rwdbf {
Public static void readdbf (cheminement de chaîne)
{
InputStream fis = null;
essayer
{
// Lire le flux d'entrée du fichier
fis = new FileInputStream (path);
// Initialisez une instance DBFreader en fonction du flux d'entrée pour lire les informations du fichier DBF
DBFReader Reader = new DBFReader (FIS);
// Appelez DBFReader pour obtenir le nombre de champs dans le fichier de chemin vers la méthode d'instance
int FieldsCount = reader.getFieldCount ();
System.out.println ("Nombre de champs:" + FieldsCount);
// Répondre aux informations sur le terrain
pour (int i = 0; i <fieldsCount; i ++)
{
Dbffield field = reader.getField (i);
System.out.println (Field.GetName ());
}
Objet [] RowValues;
// Retrouvez le fichier de chemin un par un et enregistrez
while ((RowValues = Reader.NExTRecord ())! = NULL)
{
for (int i = 0; i <rowvalues.length; i ++)
{
System.out.println (RowValues [i]);
}
}
}
Catch (exception e)
{
e.printStackTrace ();
}
Enfin
{
essayer{
fis.close ();
} catch (exception e) {}
}
}
public static void writedbf (String Path)
{
OutputStream fos = null;
essayer
{
// Définir les champs de fichiers DBF
Dbffield [] fields = new dbffield [3];
// Définissez chaque information de champ séparément, setFieldName et setName fonction de la même chose.
// c'est juste que SetFieldName n'est plus recommandé d'utiliser
champs [0] = new dbffield ();
//[email protected]("emp_code ");
fields [0] .SetName ("semp_code");
fields [0] .setDataType (dbffield.field_type_c);
champs [0] .SetFieldLength (10);
champs [1] = new dbffield ();
//fields ·1
fields [1] .setName ("emp_name");
champs [1] .setDatatype (dbffield.field_type_c);
champs [1] .setFieldLength (20);
champs [2] = new dbffield ();
//fields fichier 2.SetFieldname("salary ");
champs [2] .setName ("salaire");
fields [2] .setDataType (dbffield.field_type_n);
champs [2] .SetFieldLength (12);
champs [2] .setDeCImalCount (2);
// dbfwriter writer = new DBFWriter (nouveau fichier (chemin));
// Définissez l'instance DBFWriter pour écrire des fichiers DBF
Dbfwriter writer = new dbfwriter ();
// Écrivez des informations sur le champ dans l'instance DBFWriter, c'est-à-dire définir la structure du tableau
writer.setFields (champs);
// Rédiger des enregistrements
Objet [] rowData = nouvel objet [3];
RowData [0] = "1000";
RowData [1] = "John";
RowData [2] = nouveau double (5000,00);
writer.AddreCord (RowData);
rowData = nouvel objet [3];
RowData [0] = "1001";
RowData [1] = "Lalit";
RowData [2] = nouveau double (3400,00);
writer.AddreCord (RowData);
rowData = nouvel objet [3];
RowData [0] = "1002";
RowData [1] = "Rohit";
RowData [2] = nouveau double (7350,00);
writer.AddreCord (RowData);
// définir le flux de sortie et associer un fichier
fos = new FileOutputStream (path);
// Écrivez des données
écrivain.write (FOS);
//writer.write ();
} catch (exception e)
{
e.printStackTrace ();
}
Enfin
{
essayer{
fos.close ();
} catch (exception e) {}
}
}
public static void main (String [] args) {
String path = "e: //tmp//2//xx.dbf";
essayer {
InputStream fis = new FileInputStream (path);
DBFReader Reader = new DBFReader (FIS);
int FieldsCount = reader.getFieldCount ();
System.out.println ("Nombre de champs:" + FieldsCount);
Dbffield [] df = new dbffield [fieldsCount + 2];
pour (int i = 0; i <fieldsCount; i ++)
{
df [i] = reader.getField (i);
System.out.println ("champ" + i + ":" + df [i] .getName ());
}
df [FieldsCount] = new Dbffield ();
df [FieldsCount] .SetName ("add1");
df [FieldsCount] .SetDatatype (dbffield.field_type_c);
df [FieldsCount] .SetFieldLength (10);
df [FieldsCount + 1] = new Dbffield ();
df [FieldsCount + 1] .SetName ("add2");
df [FieldsCount + 1] .SetDatatype (dbffield.field_type_c);
df [FieldsCount + 1] .SetFieldLength (10);
Dbfwriter writer = new dbfwriter ();
writer.setFields (DF);
Objet [] RowValues;
Objet [] rowValues1 = nouvel objet [FieldsCount + 2];
// Retrouvez le fichier de chemin un par un et enregistrez
while ((RowValues = Reader.NExTRecord ())! = NULL)
{
pour (int i = 0; i <fieldsCount; i ++) {
RowValues1 [i] = rowValues [i];
}
RowValues1 [FieldsCount] = "x";
RowValues1 [FieldsCount + 1] = "xx";
écrivain.AdDrecord (RowValues1);
}
path = "e: //tmp//2//test2.dbf";
OutputStream fos = new FileOutputStream (path);
// Écrivez des données
écrivain.write (FOS);
System.out.println ("Over");
} catch (filenotfoundException | dbfexception e) {
// Bloc de capture généré automatiquement de TODO
e.printStackTrace ();
}
}
}