Java parse DBF generates and reads DBF files via JAVADBF packets _java

Source: Internet
Author: User
Tags instance method

Here's a simple example

Copy Code code as follows:

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
{
Reading the input stream of a file
FIS = new FileInputStream (path);
Initializes a dbfreader instance from the input stream to read DBF file information
Dbfreader reader = new Dbfreader (FIS);
Call Dbfreader to get the number of fields in the path file on an instance method
int fieldscount = Reader.getfieldcount ();
System.out.println ("Number of fields:" +fieldscount);
Remove field information
for (int i=0; i<fieldscount; i++)
{
Dbffield field = Reader.getfield (i);
System.out.println (Field.getname ());
}
Object[] rowvalues;
Strips out of the path file record
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
{
Define DBF file fields
dbffield[] fields = new Dbffield[3];
Define each field information separately, Setfieldname and SetName function the same,
It's just that Setfieldname has not recommended using
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));

Define Dbfwriter instance to write DBF file
Dbfwriter writer = new Dbfwriter ();
Write the field information to the Dbfwriter instance, which defines the table structure
Writer.setfields (fields);
The writing records of the strips
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);

Defines the output stream and associates a file
FOS = new FileOutputStream (path);
Write Data
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 ("Number of fields:" +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];
Strips out of the path file record
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);
Write Data
Writer.write (FOS);

System.out.println ("over");


catch (FileNotFoundException | Dbfexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.