Java讀取DBF文件,需要載入開源jar包JavaDBF.jar,然後使用裡面的方法進行讀寫,示例如下:
packagesearch;
publicclassReadDBF{
publicstaticvoidmain(Stringargs[])
{
readDBF();
}
privatestaticvoidreadDBF()
{
try
{
System.out.println("正在讀取文件!");
DBFReaderdbfreader=newDBFReader("D:\ZG2011格式.dbf");
for(intb=0;b<dbfreader.getFieldCount();b++)
{
if(b>0)
{System.out.print(",");}
System.out.print(dbfreader.getField(b).getName().trim().toUpperCase());
if(b==(dbfreader.getFieldCount()-1))
System.out.print(" ");
}
for(inti=0;dbfreader.hasNextRecord();i++)
{
String[]aobj=dbfreader.nextRecordString();
for(intb=0;b<dbfreader.getFieldCount();b++)
{
if(b>0)
System.out.print(",");
System.out.print(aobj[b].trim());
if(b==(dbfreader.getFieldCount()-1))
System.out.print(" ");
}
}
System.out.println("讀取文件成功!");
}
catch(Exceptione)
{
System.out.print(e.getMessage());
}
}
}
publicclassRwdbf{
publicstaticvoidreadDBF(Stringpath)
{
InputStreamfis=null;
try
{
//讀取文件的輸入流
fis=newFileInputStream(path);
//根據輸入流初始化一個DBFReader實例,用來讀取DBF文件信息
DBFReaderreader=newDBFReader(fis);
//調用DBFReader對實例方法得到path文件中欄位的個數
intfieldsCount=reader.getFieldCount();
System.out.println("欄位數:"+fieldsCount);
//取出欄位信息
for(inti=0;i<fieldsCount;i++)
{
DBFFieldfield=reader.getField(i);
System.out.println(field.getName());
}
Object[]rowValues;
//一條條取出path文件中記錄
while((rowValues=reader.nextRecord())!=null)
{
for(inti=0;i<rowValues.length;i++)
{
System.out.println(rowValues[i]);
}
}
}
catch(Exceptione)
{
e.printStackTrace();
}
finally
{
try{
fis.close();
}catch(Exceptione){}
}
}publicstaticvoidwriteDBF(Stringpath)
{
OutputStreamfos=null;
try
{
//定義DBF文件欄位
DBFField[]fields=newDBFField[3];
//分別定義各個欄位信息,setFieldName和setName作用相同,
//只是setFieldName已經不建議使用
fields[0]=newDBFField();
//fields[0].setFieldName("emp_code");
fields[0].setName("semp_code");
fields[0].setDataType(DBFField.FIELD_TYPE_C);
fields[0].setFieldLength(10);
fields[1]=newDBFField();
//fields[1].setFieldName("emp_name");
fields[1].setName("emp_name");
fields[1].setDataType(DBFField.FIELD_TYPE_C);
fields[1].setFieldLength(20);
fields[2]=newDBFField();
//fields[2].setFieldName("salary");
fields[2].setName("salary");
fields[2].setDataType(DBFField.FIELD_TYPE_N);
fields[2].setFieldLength(12);
fields[2].setDecimalCount(2);
//DBFWriterwriter=newDBFWriter(newFile(path));
//定義DBFWriter實例用來寫DBF文件
DBFWriterwriter=newDBFWriter();
//把欄位信息寫入DBFWriter實例,即定義表結構
writer.setFields(fields);
//一條條的寫入記錄
Object[]rowData=newObject[3];
rowData[0]="1000";
rowData[1]="John";
rowData[2]=newDouble(5000.00);
writer.addRecord(rowData);
rowData=newObject[3];
rowData[0]="1001";
rowData[1]="Lalit";
rowData[2]=newDouble(3400.00);
writer.addRecord(rowData);
rowData=newObject[3];
rowData[0]="1002";
rowData[1]="Rohit";
rowData[2]=newDouble(7350.00);
writer.addRecord(rowData);
//定義輸出流,並關聯的一個文件
fos=newFileOutputStream(path);
//寫入數據
writer.write(fos);
//writer.write();
}catch(Exceptione)
{
e.printStackTrace();
}
finally
{
try{
fos.close();
}catch(Exceptione){}
}
}
publicstaticvoidmain(String[]args){
Stringpath="E:\tmp\2\xx.dbf";
try{
InputStreamfis=newFileInputStream(path);
DBFReaderreader=newDBFReader(fis);
intfieldsCount=reader.getFieldCount();
System.out.println("欄位數:"+fieldsCount);
DBFField[]df=newDBFField[fieldsCount+2];
for(inti=0;i<fieldsCount;i++)
{
df[i]=reader.getField(i);
System.out.println("field"+i+":"+df[i].getName());
}
df[fieldsCount]=newDBFField();
df[fieldsCount].setName("add1");
df[fieldsCount].setDataType(DBFField.FIELD_TYPE_C);
df[fieldsCount].setFieldLength(10);
df[fieldsCount+1]=newDBFField();
df[fieldsCount+1].setName("add2");
df[fieldsCount+1].setDataType(DBFField.FIELD_TYPE_C);
df[fieldsCount+1].setFieldLength(10);
DBFWriterwriter=newDBFWriter();
writer.setFields(df);
Object[]rowValues;
Object[]rowValues1=newObject[fieldsCount+2];
//一條條取出path文件中記錄
while((rowValues=reader.nextRecord())!=null)
{
for(inti=0;i<fieldsCount;i++){
rowValues1[i]=rowValues[i];
}
rowValues1[fieldsCount]="x";
rowValues1[fieldsCount+1]="xx";
writer.addRecord(rowValues1);
}
path="E:\tmp\2\test2.dbf";
OutputStreamfos=newFileOutputStream(path);
//寫入數據
writer.write(fos);
System.out.println("OVER");
}catch(FileNotFoundException|DBFExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
詳細出處參考:http://www.jb51.net/article/46344.htm