欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java解析dbf之通过javadbf包生成和读取dbf文件

程序员文章站 2024-02-23 18:13:34
以下是简单示例 复制代码 代码如下:package com.cramc; import java.io.fileinputstream;import java.io.f...

以下是简单示例

复制代码 代码如下:

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();
  }
 }

}