java解析dbf之通过javadbf包生成和读取dbf文件
作者:
以下是简单示例
package com.cramc;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import 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;
public class Rwdbf {
public static void readDBF(String path)
{
InputStream fis = null;
try
{
//读取文件的输入流
fis = new FileInputStream(path);
//根据输入流初始化一个DBFReader实例,用来读取DBF文件信息
DBFReader reader = new DBFReader(fis);
//调用DBFReader对实例方法得到path文件中字段的个数
int fieldsCount = reader.getFieldCount();
System.out.println("字段数:"+fieldsCount);
//取出字段信息
for( int i=0; i<fieldsCount; i++)
{
DBFField field = reader.getField(i);
System.out.println(field.getName());
}
Object[] rowValues;
//一条条取出path文件中记录
while((rowValues = reader.nextRecord()) != null)
{
for( int i=0; i<rowValues.length; i++)
{
System.out.println(rowValues[i]);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try{
fis.close();
}catch(Exception e){}
}
}
public static void writeDBF(String path)
{
OutputStream fos = null;
try
{
//定义DBF文件字段
DBFField[] fields = new DBFField[3];
//分别定义各个字段信息,setFieldName和setName作用相同,
//只是setFieldName已经不建议使用
fields[0] = new DBFField();
//fields[0].setFieldName("emp_code");
fields[0].setName("semp_code");
fields[0].setDataType(DBFField.FIELD_TYPE_C);
fields[0].setFieldLength(10);
fields[1] = new DBFField();
//fields[1].setFieldName("emp_name");
fields[1].setName("emp_name");
fields[1].setDataType(DBFField.FIELD_TYPE_C);
fields[1].setFieldLength(20);
fields[2] = new DBFField();
//fields[2].setFieldName("salary");
fields[2].setName("salary");
fields[2].setDataType(DBFField.FIELD_TYPE_N);
fields[2].setFieldLength(12);
fields[2].setDecimalCount(2);
//DBFWriter writer = new DBFWriter(new File(path));
//定义DBFWriter实例用来写DBF文件
DBFWriter writer = new DBFWriter();
//把字段信息写入DBFWriter实例,即定义表结构
writer.setFields(fields);
//一条条的写入记录
Object[] rowData = new Object[3];
rowData[0] = "1000";
rowData[1] = "John";
rowData[2] = new Double(5000.00);
writer.addRecord(rowData);
rowData = new Object[3];
rowData[0] = "1001";
rowData[1] = "Lalit";
rowData[2] = new Double(3400.00);
writer.addRecord(rowData);
rowData = new Object[3];
rowData[0] = "1002";
rowData[1] = "Rohit";
rowData[2] = new Double(7350.00);
writer.addRecord(rowData);
//定义输出流,并关联的一个文件
fos = new FileOutputStream(path);
//写入数据
writer.write(fos);
//writer.write();
}catch(Exception e)
{
e.printStackTrace();
}
finally
{
try{
fos.close();
}catch(Exception e){}
}
}
public static void main(String[] args){
String path ="E:\\tmp\\2\\xx.dbf";
try {
InputStream fis = new FileInputStream(path);
DBFReader reader = new DBFReader(fis);
int fieldsCount = reader.getFieldCount();
System.out.println("字段数:"+fieldsCount);
DBFField[] df = new DBFField[fieldsCount+2];
for( int i=0; i<fieldsCount; i++)
{
df[i] = reader.getField(i);
System.out.println("field"+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);
Object[] rowValues;
Object[] rowValues1 = new Object[fieldsCount+2];
//一条条取出path文件中记录
while((rowValues = reader.nextRecord()) != null)
{
for(int i=0;i<fieldsCount;i++){
rowValues1[i] = rowValues[i];
}
rowValues1[fieldsCount]="x";
rowValues1[fieldsCount+1]="xx";
writer.addRecord(rowValues1);
}
path ="E:\\tmp\\2\\test2.dbf";
OutputStream fos = new FileOutputStream(path);
//写入数据
writer.write(fos);
System.out.println("OVER");
} catch (FileNotFoundException | DBFException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
您可能感兴趣的文章:
- java文件对话框过滤特定文件类型示例
- java读取文件字符集示例方法
- java复制文件和java移动文件的示例分享
- java按指定编码写入和读取文件内容的类分享
- java自动根据文件内容的编码来读取避免乱码
- java写入文件的几种方法分享
- java清除u盘内存卡里的垃圾文件示例
- java读取excel文件并复制(copy)文件到指定目录示例
- Java生成和解析XML格式文件和字符串的实例代码
- java使用ftp上传文件示例分享
- java操作ftp下载文件示例
- Javascript写入txt和读取txt文件示例
- java文件操作工具类分享(file文件工具类)
- java读取文件显示进度条的实现方法
- java制作复制文件工具代码分享
- java生成csv文件乱码的解决方法示例 java导出csv乱码
- java数字图像处理基础使用imageio写图像文件示例
- java删除文件夹下所有文件示例分享