您的位置:首页 > 编程语言 > Java开发

javadbf 实现解决中文乱码问题

2012-04-22 09:59 501 查看
DBFBase.java

public abstract class DBFBase {

protected String characterSetName = "8859_1";

protected final int END_OF_DATA = 0x1A;

/*

If the library is used in a non-latin environment use this method to set

corresponding character set. More information:

http://www.iana.org/assignments/character-sets

Also see the documentation of the class java.nio.charset.Charset

*/

public String getCharactersetName() {

return this.characterSetName;

}

public void setCharactersetName( String characterSetName) {

this.characterSetName = characterSetName;

}

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

DBFException.java

import java.io.IOException;

public class DBFException extends IOException {

/**

*

*/

private static final long serialVersionUID = -883243652959406984L;

public DBFException() {

super();

}

public DBFException( String msg) {

super( msg);

}

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

DBFFactory.java

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.HashMap;

import org.apache.log4j.Logger;

import com.ddit.serviceflat.db.DBConnection;

public class DBFFactory {

static Logger logger = (Logger)Logger.getLogger(DBFFactory.class.getName());

public static void main(String args[]) {

String path = "D:\\viwoqu\\200709hzfk\\fxgmms.dbf";

readDBF(path);

}

public static void readDBF(String path) {

InputStream fis = null;

try {

// 读取文件的输入流

fis = new FileInputStream(path);

// 根据输入流初始化一个DBFReader实例,用来读取DBF文件信息

DBFReader reader = new DBFReader(fis);

reader.setCharactersetName("GBK");

// 调用DBFReader对实例方法得到path文件中字段的个数

int fieldsCount = reader.getFieldCount();

// 取出字段信息

for (int i = 0; i < fieldsCount; i++) {

DBFField field = reader.getField(i);

}

Object[] rowValues;

// 一条条取出path文件中记录

while ((rowValues = reader.nextRecord()) != null) {

for (int i = 0; i < rowValues.length; 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[0].setDecimalCount(0);

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[1].setDecimalCount(0);

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 int readDBFToTable(String path, String tableName) {

ArrayList insertSqlList = readDBFToTableInsertSQL(path, tableName);

Connection conn = null;

PreparedStatement ps = null;

int successCount = 0;

try {

conn = DBConnection.getWMConnection();

for (int i = 0; i < insertSqlList.size(); i++) {

try {

ps = conn.prepareStatement(insertSqlList.get(i).toString());

ps.executeUpdate();

successCount++;

} catch (Exception e) {

logger.error(insertSqlList.get(i));

logger.error("忽略一条错误");

}

}

} catch (Exception e) {

e.printStackTrace();

}

finally {

if (conn != null) {

try

{

ps.close();

conn.close();

}

catch (SQLException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

return successCount;

}

public static ArrayList readDBFToTableInsertSQL(String path,

String tableName) {

HashMap resultMap = readDBFToHashMap(path);

String[] fieldNames = (String[]) resultMap.get("fieldNames");

ArrayList rowList = (ArrayList) resultMap.get("rowList");

String insertSql = "insert into " + tableName + "(";

ArrayList insertSqlList = new ArrayList();

for (int i = 0; i < fieldNames.length - 1; i++) {

insertSql += fieldNames[i] + ",";

}

insertSql = insertSql.substring(0, insertSql.length() - 1);

insertSql += ") values ";

for (int i = 0; i < rowList.size(); i++) {

String rowValueSql = "";

String finalSql = "";

Object[] row = (Object[]) rowList.get(i);

for (int j = 0; j < row.length - 1; j++) {

rowValueSql += "'" + row[j] + "',";

}

rowValueSql = "("

+ rowValueSql.substring(0, rowValueSql.length() - 1) + ")";

finalSql = insertSql + rowValueSql;

insertSqlList.add(finalSql);

}

return insertSqlList;

}

public static HashMap readDBFToHashMap(String path) {

HashMap resultMap = new HashMap();

InputStream fis = null;

try {

fis = new FileInputStream(path);

DBFReader reader = new DBFReader(fis);

reader.setCharactersetName("GBK");

int fieldsCount = reader.getFieldCount();

String[] fieldNames = new String[fieldsCount];

Object[] rowValues;

ArrayList rowList = new ArrayList();

for (int i = 0; i < fieldsCount; i++) {

DBFField field = reader.getField(i);

fieldNames[i] = field.getName();

}

while ((rowValues = reader.nextRecord()) != null) {

rowList.add(rowValues);

}

resultMap.put("fieldNames", fieldNames);

resultMap.put("rowList", rowList);

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

fis.close();

} catch (Exception e) {

}

}

return resultMap;

}

public static void generateDBFFromSql(String sql, String path) {

Connection conn = null;

ResultSet rs = null;

ResultSetMetaData meta = null;

int columnCount = 0;

int recordCount = 0;

Object[][] data = null;

String[] strutName;

int[] strutLength;

int[] strutScale;

byte[] strutType;

try {

if (sql.split("from")[1].trim().startsWith("cpcode")) {

conn = DBConnection.getYCSBConnection();

} else {

conn = DBConnection.getWMConnection();

}

PreparedStatement ps = conn.prepareStatement(sql,

ResultSet.TYPE_SCROLL_INSENSITIVE,

ResultSet.CONCUR_READ_ONLY);

rs = ps.executeQuery();

meta = rs.getMetaData();

columnCount = meta.getColumnCount();

rs.last();

recordCount = rs.getRow();

rs.first();

strutName = new String[columnCount];

strutLength = new int[columnCount];

strutScale = new int[columnCount];

strutType = new byte[columnCount];

data = new Object[recordCount][columnCount];

for (int i = 0; i < columnCount; i++) {

strutName[i] = meta.getColumnName(i + 1);

strutLength[i] = meta.getPrecision(i + 1);

strutScale[i] = meta.getScale(i + 1);

String columnName = meta.getColumnTypeName(i + 1);

if (columnName.indexOf("char") != -1) {

strutType[i] = DBFField.FIELD_TYPE_C;

} else if (columnName.indexOf("num") != -1) {

strutType[i] = DBFField.FIELD_TYPE_N;

} else if (columnName.indexOf("date") != -1) {

strutType[i] = DBFField.FIELD_TYPE_D;

} else if (columnName.indexOf("bit") != -1) {

strutType[i] = DBFField.FIELD_TYPE_L;

} else if (columnName.indexOf("text") != -1) {

strutType[i] = DBFField.FIELD_TYPE_C;

strutLength[i] = 2000;

} else {

strutType[i] = DBFField.FIELD_TYPE_C;

}

}

for (int i = 0; i < recordCount; i++) {

for (int j = 0; j < columnCount; j++) {

data[i][j] = rs.getObject(j + 1);

}

rs.next();

}

generateDbfFromArray(path, strutName, strutType, strutLength,

strutScale, data);

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if(rs !=null){

rs.close();

}

if (conn != null) {

conn.close();

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

private static void generateDbfFromArray(String dbfName,

String[] strutName, byte[] strutType, int[] strutLength,

int[] strutScale, Object[][] data) {

OutputStream fos = null;

try {

int columnCount = strutName.length;

int recordCount = data.length;

DBFField[] fields = new DBFField[columnCount];

for (int i = 0; i < fields.length; i++) {

fields[i] = new DBFField();

fields[i].setName(strutName[i]);

fields[i].setDataType(strutType[i]);

if (strutType[i] != DBFField.FIELD_TYPE_D) {

fields[i].setFieldLength(strutLength[i]);

fields[i].setDecimalCount(strutScale[i]);

}

}

DBFWriter writer = new DBFWriter();

// writer.setCharactersetName("GBK");

writer.setFields(fields);

for (int i = 0; i < recordCount; i++) {

writer.addRecord(data[i]);

}

fos = new FileOutputStream(dbfName);

writer.write(fos);

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

fos.close();

} catch (Exception e) {

}

}

}

private static void ResultsetToArray(ResultSet rs) {

try {

ResultSetMetaData meta = rs.getMetaData();

int columnCount = meta.getColumnCount();

String[] strutName = new String[columnCount];

byte[] strutType = new byte[columnCount];

rs.last();

int itemCount = rs.getRow();

rs.first();

Object[][] data = new Object[columnCount][itemCount];

for (int i = 0; i < columnCount; i++) {

strutType[i] = (byte) meta.getColumnType(i);

strutName[i] = meta.getColumnName(i);

}

for (int i = 0; rs.next(); i++) {

for (int j = 0; j < columnCount; j++) {

data[i][j] = rs.getObject(j);

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

DBFField.java

import java.io.*;

/**

DBFField represents a field specification in an dbf file.

DBFField objects are either created and added to a DBFWriter object or obtained

from DBFReader object through getField( int) query.

*/

public class DBFField {

public static final byte FIELD_TYPE_C = (byte)'C';

public static final byte FIELD_TYPE_L = (byte)'L';

public static final byte FIELD_TYPE_N = (byte)'N';

public static final byte FIELD_TYPE_F = (byte)'F';

public static final byte FIELD_TYPE_D = (byte)'D';

public static final byte FIELD_TYPE_M = (byte)'M';

/* Field struct variables start here */

byte[] fieldName = new byte[ 11]; /* 0-10*/

byte dataType; /* 11 */

int reserv1; /* 12-15 */

int fieldLength; /* 16 */

byte decimalCount; /* 17 */

short reserv2; /* 18-19 */

byte workAreaId; /* 20 */

short reserv3; /* 21-22 */

byte setFieldsFlag; /* 23 */

byte[] reserv4 = new byte[ 7]; /* 24-30 */

byte indexFieldFlag; /* 31 */

/* Field struct variables end here */

/* other class variables */

int nameNullIndex = 0;

/**

Creates a DBFField object from the data read from the given DataInputStream.

The data in the DataInputStream object is supposed to be organised correctly

and the stream "pointer" is supposed to be positioned properly.

@param in DataInputStream

@return Returns the created DBFField object.

@throws IOException If any stream reading problems occures.

*/

protected static DBFField createField( DataInput in)

throws IOException {

DBFField field = new DBFField();

byte t_byte = in.readByte(); /* 0 */

if( t_byte == (byte)0x0d) {

return null;

}

in.readFully( field.fieldName, 1, 10); /* 1-10 */

field.fieldName[0] = t_byte;

for( int i=0; i<field.fieldName.length; i++) {

if( field.fieldName[ i] == (byte)0) {

field.nameNullIndex = i;

break;

}

}

field.dataType = in.readByte(); /* 11 */

field.reserv1 = Utils.readLittleEndianInt( in); /* 12-15 */

field.fieldLength = in.readUnsignedByte(); /* 16 */

field.decimalCount = in.readByte(); /* 17 */

field.reserv2 = Utils.readLittleEndianShort( in); /* 18-19 */

field.workAreaId = in.readByte(); /* 20 */

field.reserv2 = Utils.readLittleEndianShort( in); /* 21-22 */

field.setFieldsFlag = in.readByte(); /* 23 */

in.readFully( field.reserv4); /* 24-30 */

field.indexFieldFlag = in.readByte(); /* 31 */

return field;

}

/**

Writes the content of DBFField object into the stream as per

DBF format specifications.

@param os OutputStream

@throws IOException if any stream related issues occur.

*/

protected void write( DataOutput out)

throws IOException {

//DataOutputStream out = new DataOutputStream( os);

// Field Name

out.write( fieldName); /* 0-10 */

out.write( new byte[ 11 - fieldName.length]);

// data type

out.writeByte( dataType); /* 11 */

out.writeInt( 0x00); /* 12-15 */

out.writeByte( fieldLength); /* 16 */

out.writeByte( decimalCount); /* 17 */

out.writeShort( (short)0x00); /* 18-19 */

out.writeByte( (byte)0x00); /* 20 */

out.writeShort( (short)0x00); /* 21-22 */

out.writeByte( (byte)0x00); /* 23 */

out.write( new byte[7]); /* 24-30*/

out.writeByte( (byte)0x00); /* 31 */

}

/**

Returns the name of the field.

@return Name of the field as String.

*/

public String getName() {

return new String( this.fieldName, 0, nameNullIndex);

}

/**

Returns the data type of the field.

@return Data type as byte.

*/

public byte getDataType() {

return dataType;

}

/**

Returns field length.

@return field length as int.

*/

public int getFieldLength() {

return fieldLength;

}

/**

Returns the decimal part. This is applicable

only if the field type if of numeric in nature.

If the field is specified to hold integral values

the value returned by this method will be zero.

@return decimal field size as int.

*/

public int getDecimalCount() {

return decimalCount;

}

// Setter methods

// byte[] fieldName = new byte[ 11]; /* 0-10*/

// byte dataType; /* 11 */

// int reserv1; /* 12-15 */

// byte fieldLength; /* 16 */

// byte decimalCount; /* 17 */

// short reserv2; /* 18-19 */

// byte workAreaId; /* 20 */

// short reserv3; /* 21-22 */

// byte setFieldsFlag; /* 23 */

// byte[] reserv4 = new byte[ 7]; /* 24-30 */

// byte indexFieldFlag; /* 31 */

/**

* @deprecated This method is depricated as of version 0.3.3.1 and is replaced by{@link #setName( String)}.

*/

public void setFieldName( String value) {

setName( value);

}

/**

Sets the name of the field.

@param name of the field as String.

@since 0.3.3.1

*/

public void setName( String value) {

if( value == null) {

throw new IllegalArgumentException( "Field name cannot be null");

}

if( value.length() == 0 || value.length() > 10) {

throw new IllegalArgumentException( "Field name should be of length 0-10");

}

this.fieldName = value.getBytes();

this.nameNullIndex = this.fieldName.length;

}

/**

Sets the data type of the field.

@param type of the field. One of the following:<br>

C, L, N, F, D, M

*/

public void setDataType( byte value) {

switch( value) {

case 'D':

this.fieldLength = 8; /* fall through */

case 'C':

case 'L':

case 'N':

case 'F':

case 'M':

this.dataType = value;

break;

default:

throw new IllegalArgumentException( "Unknown data type");

}

}

/**

Length of the field.

This method should be called before calling setDecimalCount().

@param Length of the field as int.

*/

public void setFieldLength( int value) {

if( value <= 0) {

throw new IllegalArgumentException( "Field length should be a positive number");

}

if( this.dataType == FIELD_TYPE_D) {

throw new UnsupportedOperationException( "Cannot do this on a Date field");

}

fieldLength = value;

}

/**

Sets the decimal place size of the field.

Before calling this method the size of the field

should be set by calling setFieldLength().

@param Size of the decimal field.

*/

public void setDecimalCount( int value) {

if( value < 0) {

throw new IllegalArgumentException( "Decimal length should be a positive number");

}

if( value > fieldLength) {

throw new IllegalArgumentException( "Decimal length should be less than field length");

}

decimalCount = (byte)value;

}

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

DBFHeader.java

import java.io.*;

import java.util.*;

class DBFHeader {

static final byte SIG_DBASE_III = (byte)0x03;

/* DBF structure start here */

byte signature; /* 0 */

byte year; /* 1 */

byte month; /* 2 */

byte day; /* 3 */

int numberOfRecords; /* 4-7 */

short headerLength; /* 8-9 */

short recordLength; /* 10-11 */

short reserv1; /* 12-13 */

byte incompleteTransaction; /* 14 */

byte encryptionFlag; /* 15 */

int freeRecordThread; /* 16-19 */

int reserv2; /* 20-23 */

int reserv3; /* 24-27 */

byte mdxFlag; /* 28 */

byte languageDriver; /* 29 */

short reserv4; /* 30-31 */

DBFField []fieldArray; /* each 32 bytes */

byte terminator1; /* n+1 */

//byte[] databaseContainer; /* 263 bytes */

/* DBF structure ends here */

DBFHeader() {

this.signature = SIG_DBASE_III;

this.terminator1 = 0x0D;

}

void read( DataInput dataInput) throws IOException {

signature = dataInput.readByte(); /* 0 */

year = dataInput.readByte(); /* 1 */

month = dataInput.readByte(); /* 2 */

day = dataInput.readByte(); /* 3 */

numberOfRecords = Utils.readLittleEndianInt( dataInput); /* 4-7 */

headerLength = Utils.readLittleEndianShort( dataInput); /* 8-9 */

recordLength = Utils.readLittleEndianShort( dataInput); /* 10-11 */

reserv1 = Utils.readLittleEndianShort( dataInput); /* 12-13 */

incompleteTransaction = dataInput.readByte(); /* 14 */

encryptionFlag = dataInput.readByte(); /* 15 */

freeRecordThread = Utils.readLittleEndianInt( dataInput); /* 16-19 */

reserv2 = dataInput.readInt(); /* 20-23 */

reserv3 = dataInput.readInt(); /* 24-27 */

mdxFlag = dataInput.readByte(); /* 28 */

languageDriver = dataInput.readByte(); /* 29 */

reserv4 = Utils.readLittleEndianShort( dataInput); /* 30-31 */

Vector v_fields = new Vector();

DBFField field = DBFField.createField( dataInput); /* 32 each */

while( field != null) {

v_fields.addElement( field);

field = DBFField.createField( dataInput);

}

fieldArray = new DBFField[ v_fields.size()];

for( int i=0; i<fieldArray.length; i++) {

fieldArray[ i] = (DBFField)v_fields.elementAt( i);

}

}

void write( DataOutput dataOutput) throws IOException {

dataOutput.writeByte( signature); /* 0 */

GregorianCalendar calendar = new GregorianCalendar();

year = (byte)( calendar.get( Calendar.YEAR) - 1900);

month = (byte)( calendar.get( Calendar.MONTH)+1);

day = (byte)( calendar.get( Calendar.DAY_OF_MONTH));

dataOutput.writeByte( year); /* 1 */

dataOutput.writeByte( month); /* 2 */

dataOutput.writeByte( day); /* 3 */

numberOfRecords = Utils.littleEndian( numberOfRecords);

dataOutput.writeInt( numberOfRecords); /* 4-7 */

headerLength = findHeaderLength();

dataOutput.writeShort( Utils.littleEndian( headerLength)); /* 8-9 */

recordLength = findRecordLength();

dataOutput.writeShort( Utils.littleEndian( recordLength)); /* 10-11 */

dataOutput.writeShort( Utils.littleEndian( reserv1)); /* 12-13 */

dataOutput.writeByte( incompleteTransaction); /* 14 */

dataOutput.writeByte( encryptionFlag); /* 15 */

dataOutput.writeInt( Utils.littleEndian( freeRecordThread));/* 16-19 */

dataOutput.writeInt( Utils.littleEndian( reserv2)); /* 20-23 */

dataOutput.writeInt( Utils.littleEndian( reserv3)); /* 24-27 */

dataOutput.writeByte( mdxFlag); /* 28 */

dataOutput.writeByte( languageDriver); /* 29 */

dataOutput.writeShort( Utils.littleEndian( reserv4)); /* 30-31 */

for( int i=0; i<fieldArray.length; i++) {

fieldArray[i].write( dataOutput);

}

dataOutput.writeByte( terminator1); /* n+1 */

}

private short findHeaderLength() {

return (short)(

1+

3+

4+

2+

2+

2+

1+

1+

4+

4+

4+

1+

1+

2+

(32*fieldArray.length)+

1

);

}

private short findRecordLength() {

int recordLength = 0;

for( int i=0; i<fieldArray.length; i++) {

recordLength += fieldArray[i].getFieldLength();

}

return (short)(recordLength + 1);

}

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

DBFReader.java

import java.io.*;

import java.util.*;

/**

DBFReader class can creates objects to represent DBF data.

This Class is used to read data from a DBF file. Meta data and

records can be queried against this document.

<p>

DBFReader cannot write anythng to a DBF file. For creating DBF files

use DBFWriter.

<p>

Fetching rocord is possible only in the forward direction and

cannot re-wound. In such situation, a suggested approach is to reconstruct the object.

<p>

The nextRecord() method returns an array of Objects and the types of these

Object are as follows:

<table>

<tr>

<th>xBase Type</th><th>Java Type</th>

</tr>

<tr>

<td>C</td><td>String</td>

</tr>

<tr>

<td>N</td><td>Integer</td>

</tr>

<tr>

<td>F</td><td>Double</td>

</tr>

<tr>

<td>L</td><td>Boolean</td>

</tr>

<tr>

<td>D</td><td>java.util.Date</td>

</tr>

</table>

*/

public class DBFReader extends DBFBase {

DataInputStream dataInputStream;

DataInputStream memoInputStream;

DBFHeader header;

int blockLength;

/* Class specific variables */

boolean isClosed = true;

/**

Initializes a DBFReader object.

When this constructor returns the object

will have completed reading the hader (meta date) and

header information can be quried there on. And it will

be ready to return the first row.

@param InputStream where the data is read from.

*/

public DBFReader( InputStream in) throws DBFException {

try

{

this.dataInputStream = new DataInputStream( in);

this.isClosed = false;

this.header = new DBFHeader();

this.header.read( this.dataInputStream);

/* it might be required to leap to the start of records at times */

int t_dataStartIndex = this.header.headerLength - ( 32 + (32*this.header.fieldArray.length)) - 1;

if( t_dataStartIndex > 0)

{

dataInputStream.skip( t_dataStartIndex);

}

}

catch( IOException e) {

throw new DBFException( e.getMessage());

}

}

public DBFReader( InputStream in, InputStream memo) throws DBFException

{

this(in);

//读入memo内容

try

{

this.memoInputStream = new DataInputStream(memo);

this.initMemoInputStream();// 初始化数据流,代码见下面

}

catch (IOException e)

{

throw new DBFException( e.getMessage());

}

}

private void initMemoInputStream() throws IOException {

memoInputStream.read(new byte[6]); // 00 - 03 下一个自由块的位置1 04 – 05 未使用

blockLength =0; //Utils.readHighEndianShort(memoInputStream); // 06 – 07 块大小(每个块的字节数)1

memoInputStream.read(new byte[504]); // 08 – 511 未使用

}

public String toString() {

StringBuffer sb = new StringBuffer( this.header.year + "/" + this.header.month + "/" + this.header.day + "\n"

+ "Total records: " + this.header.numberOfRecords +

"\nHEader length: " + this.header.headerLength +

"");

for( int i=0; i<this.header.fieldArray.length; i++) {

sb.append( this.header.fieldArray[i].getName());

sb.append( "\n");

}

return sb.toString();

}

/**

Returns the number of records in the DBF.

*/

public int getRecordCount() {

return this.header.numberOfRecords;

}

/**

Returns the asked Field. In case of an invalid index,

it returns a ArrayIndexOutofboundsException.

@param index. Index of the field. Index of the first field is zero.

*/

public DBFField getField( int index)

throws DBFException {

if( isClosed) {

throw new DBFException( "Source is not open");

}

return this.header.fieldArray[ index];

}

/**

Returns the number of field in the DBF.

*/

public int getFieldCount()

throws DBFException {

if( isClosed) {

throw new DBFException( "Source is not open");

}

if( this.header.fieldArray != null) {

return this.header.fieldArray.length;

}

return -1;

}

/**

Reads the returns the next row in the DBF stream.

@returns The next row as an Object array. Types of the elements

these arrays follow the convention mentioned in the class description.

*/

public Object[] nextRecord()

throws DBFException {

if( isClosed) {

throw new DBFException( "Source is not open");

}

Object recordObjects[] = new Object[ this.header.fieldArray.length];

try {

boolean isDeleted = false;

do {

if( isDeleted) {

dataInputStream.skip( this.header.recordLength-1);

}

int t_byte = dataInputStream.readByte();

if( t_byte == END_OF_DATA) {

return null;

}

isDeleted = ( t_byte == '*');

} while( isDeleted);

for( int i=0; i<this.header.fieldArray.length; i++)

{

switch( this.header.fieldArray[i].getDataType())

{

case 'C':

byte b_array[] = new byte[ this.header.fieldArray[i].getFieldLength()];

dataInputStream.read( b_array);

recordObjects[i] = new String( b_array, characterSetName);

break;

case 'D':

byte t_byte_year[] = new byte[ 4];

dataInputStream.read( t_byte_year);

byte t_byte_month[] = new byte[ 2];

dataInputStream.read( t_byte_month);

byte t_byte_day[] = new byte[ 2];

dataInputStream.read( t_byte_day);

try {

GregorianCalendar calendar = new GregorianCalendar(

Integer.parseInt( new String( t_byte_year)),

Integer.parseInt( new String( t_byte_month)) - 1,

Integer.parseInt( new String( t_byte_day))

);

//recordObjects[i] = calendar.getTime();

recordObjects[i] = new String(t_byte_year)+"-"+new String(t_byte_month)+"-"+new String(t_byte_day);

}

catch ( NumberFormatException e) {

/* this field may be empty or may have improper value set */

recordObjects[i] = null;

}

break;

case 'F':

try {

byte t_float[] = new byte[ this.header.fieldArray[i].getFieldLength()];

dataInputStream.read( t_float);

t_float = Utils.trimLeftSpaces( t_float);

if( t_float.length > 0 && !Utils.contains( t_float, (byte)'?')) {

recordObjects[i] = new Float( new String( t_float));

}

else {

recordObjects[i] = null;

}

}

catch( NumberFormatException e) {

throw new DBFException( "Failed to parse Float: " + e.getMessage());

}

break;

case 'N':

try {

byte t_numeric[] = new byte[ this.header.fieldArray[i].getFieldLength()];

dataInputStream.read( t_numeric);

t_numeric = Utils.trimLeftSpaces( t_numeric);

if( t_numeric.length > 0 && !Utils.contains( t_numeric, (byte)'?'))

{

//modify by viwo

// if((new String( t_numeric)).trim().length()==0)

// {

// recordObjects[i] = null;

// }

// else

// {

recordObjects[i] = new Double( new String( t_numeric));

//}

//--------------

}

else {

recordObjects[i] = null;

}

}

catch( NumberFormatException e) {

e.printStackTrace();

throw new DBFException( "Failed to parse Number: " + e.getMessage());

}

break;

case 'L':

byte t_logical = dataInputStream.readByte();

if( t_logical == 'Y' || t_logical == 't' || t_logical == 'T' || t_logical == 't') {

recordObjects[i] = Boolean.TRUE;

}

else {

recordObjects[i] = Boolean.FALSE;

}

break;

//case 'M':

// TODO Later

//recordObjects[i] = new String( "null");

//break;

case 'M':

// TODO Later

// DBF跳过memo字段

byte b_memoProxy[] = new byte[ this.header.fieldArray[i].getFieldLength()];

dataInputStream.read( b_memoProxy);

// 读取FPT字段

int iType = Utils.readHighEndianInt(memoInputStream);

if (iType == 1) {

int iLength = Utils.readHighEndianInt(memoInputStream);// 红色代码为自己添加的函数

byte b_memo[] = new byte[ iLength];

memoInputStream.read(b_memo); // 读入的FPT文件内容

recordObjects[i] = new String( b_memo, characterSetName);

if (iLength + 8 > blockLength) {

memoInputStream.read(new byte[blockLength - (iLength + 8) % blockLength]);

} else {

memoInputStream.read(new byte[blockLength - 8 - iLength]);

}

}

break;

default:

recordObjects[i] = new String( "null");

}

}

}

catch( EOFException e) {

return null;

}

catch( IOException e) {

throw new DBFException( e.getMessage());

}

return recordObjects;

}

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

DBFWriter.java

import java.io.*;

import java.util.*;

/**

An object of this class can create a DBF file.

Create an object, <br>

then define fields by creating DBFField objects and<br>

add them to the DBFWriter object<br>

add records using the addRecord() method and then<br>

call write() method.

*/

public class DBFWriter extends DBFBase {

/* other class variables */

DBFHeader header;

Vector v_records = new Vector();

int recordCount = 0;

RandomAccessFile raf = null; /* Open and append records to an existing DBF */

boolean appendMode = false;

/**

Creates an empty Object.

*/

public DBFWriter() {

this.header = new DBFHeader();

}

/**

Creates a DBFWriter which can append to records to an existing DBF file.

@param dbfFile. The file passed in shouls be a valid DBF file.

@exception Throws DBFException if the passed in file does exist but not a valid DBF file, or if an IO error occurs.

*/

public DBFWriter( File dbfFile)

throws DBFException {

try {

this.raf = new RandomAccessFile( dbfFile, "rw");

/* before proceeding check whether the passed in File object

is an empty/non-existent file or not.

*/

if( !dbfFile.exists() || dbfFile.length() == 0) {

this.header = new DBFHeader();

return;

}

header = new DBFHeader();

this.header.read( raf);

/* position file pointer at the end of the raf */

this.raf.seek( this.raf.length()-1 /* to ignore the END_OF_DATA byte at EoF */);

}

catch( FileNotFoundException e) {

throw new DBFException( "Specified file is not found. " + e.getMessage());

}

catch( IOException e) {

throw new DBFException( e.getMessage() + " while reading header");

}

this.recordCount = this.header.numberOfRecords;

}

/**

Sets fields.

*/

public void setFields( DBFField[] fields)

throws DBFException {

if( this.header.fieldArray != null) {

throw new DBFException( "Fields has already been set");

}

if( fields == null || fields.length == 0) {

throw new DBFException( "Should have at least one field");

}

for( int i=0; i<fields.length; i++) {

if( fields[i] == null) {

throw new DBFException( "Field " + (i+1) + " is null");

}

}

this.header.fieldArray = fields;

try {

if( this.raf != null && this.raf.length() == 0) {

/*

this is a new/non-existent file. So write header before proceeding

*/

this.header.write( this.raf);

}

}

catch( IOException e) {

throw new DBFException( "Error accesing file");

}

}

/**

Add a record.

*/

public void addRecord( Object[] values)

throws DBFException {

if( this.header.fieldArray == null) {

throw new DBFException( "Fields should be set before adding records");

}

if( values == null) {

throw new DBFException( "Null cannot be added as row");

}

if( values.length != this.header.fieldArray.length) {

throw new DBFException( "Invalid record. Invalid number of fields in row");

}

for( int i=0; i<this.header.fieldArray.length; i++) {

if( values[i] == null) {

continue;

}

//add by viwo to process BigDecimal problem

if(this.header.fieldArray[i].getDataType()==78)

{

if(values[i] instanceof java.math.BigDecimal)

{

values[i] = new Double(((java.math.BigDecimal)values[i]).doubleValue());

}

else

{

values[i] = Double.valueOf(values[i].toString());

}

}

//----------------

switch( this.header.fieldArray[i].getDataType()) {

case 'C':

try{

//add by viwo to process chinese problem

values[i] = new String(values[i].toString().getBytes("GBK"),"ISO-8859-1");

//--------------

}catch(Exception e){e.printStackTrace();}

if( !(values[i] instanceof String)) {

throw new DBFException( "Invalid value for field " + i);

}

break;

case 'L':

if( !( values[i] instanceof Boolean)) {

throw new DBFException( "Invalid value for field " + i);

}

break;

case 'N':

if( !( values[i] instanceof Double)) {

throw new DBFException( "Invalid value for field " + i);

}

break;

case 'D':

if( !( values[i] instanceof Date)) {

throw new DBFException( "Invalid value for field " + i);

}

break;

case 'F':

if( !(values[i] instanceof Double)) {

throw new DBFException( "Invalid value for field " + i);

}

break;

}

}

if( this.raf == null) {

v_records.addElement( values);

}

else {

try {

writeRecord( this.raf, values);

this.recordCount++;

}

catch( IOException e) {

throw new DBFException( "Error occured while writing record. " + e.getMessage());

}

}

}

/**

Writes the set data to the OutputStream.

*/

public void write( OutputStream out)

throws DBFException {

try {

if( this.raf == null) {

DataOutputStream outStream = new DataOutputStream( out);

this.header.numberOfRecords = v_records.size();

this.header.write( outStream);

/* Now write all the records */

int t_recCount = v_records.size();

for( int i=0; i<t_recCount; i++) { /* iterate through records */

Object[] t_values = (Object[])v_records.elementAt( i);

writeRecord( outStream, t_values);

}

outStream.write( END_OF_DATA);

outStream.flush();

}

else {

/* everything is written already. just update the header for record count and the END_OF_DATA mark */

this.header.numberOfRecords = this.recordCount;

this.raf.seek( 0);

this.header.write( this.raf);

this.raf.seek( raf.length());

this.raf.writeByte( END_OF_DATA);

this.raf.close();

}

}

catch( IOException e) {

throw new DBFException( e.getMessage());

}

}

public void write()

throws DBFException {

this.write( null);

}

private void writeRecord( DataOutput dataOutput, Object []objectArray)

throws IOException {

dataOutput.write( (byte)' ');

for( int j=0; j<this.header.fieldArray.length; j++) { /* iterate throught fields */

switch( this.header.fieldArray[j].getDataType()) {

case 'C':

if( objectArray[j] != null) {

String str_value = objectArray[j].toString();

dataOutput.write( Utils.textPadding( str_value, characterSetName, this.header.fieldArray[j].getFieldLength()));

}

else {

dataOutput.write( Utils.textPadding( "", this.characterSetName, this.header.fieldArray[j].getFieldLength()));

}

break;

case 'D':

if( objectArray[j] != null) {

GregorianCalendar calendar = new GregorianCalendar();

calendar.setTime( (Date)objectArray[j]);

StringBuffer t_sb = new StringBuffer();

dataOutput.write( String.valueOf( calendar.get( Calendar.YEAR)).getBytes());

dataOutput.write( Utils.textPadding( String.valueOf( calendar.get( Calendar.MONTH)+1), this.characterSetName, 2, Utils.ALIGN_RIGHT, (byte)'0'));

dataOutput.write( Utils.textPadding( String.valueOf( calendar.get( Calendar.DAY_OF_MONTH)), this.characterSetName, 2, Utils.ALIGN_RIGHT, (byte)'0'));

}

else {

dataOutput.write( " ".getBytes());

}

break;

case 'F':

if( objectArray[j] != null) {

dataOutput.write( Utils.doubleFormating( (Double)objectArray[j], this.characterSetName, this.header.fieldArray[j].getFieldLength(), this.header.fieldArray[j].getDecimalCount()));

}

else {

dataOutput.write( Utils.textPadding( "?", this.characterSetName, this.header.fieldArray[j].getFieldLength(), Utils.ALIGN_RIGHT));

}

break;

case 'N':

if( objectArray[j] != null) {

dataOutput.write(

Utils.doubleFormating( (Double)objectArray[j], this.characterSetName, this.header.fieldArray[j].getFieldLength(), this.header.fieldArray[j].getDecimalCount()));

}

else {

dataOutput.write(

Utils.textPadding( "?", this.characterSetName, this.header.fieldArray[j].getFieldLength(), Utils.ALIGN_RIGHT));

}

break;

case 'L':

if( objectArray[j] != null) {

if( (Boolean)objectArray[j] == Boolean.TRUE) {

dataOutput.write( (byte)'T');

}

else {

dataOutput.write((byte)'F');

}

}

else {

dataOutput.write( (byte)'?');

}

break;

case 'M':

break;

default:

throw new DBFException( "Unknown field type " + this.header.fieldArray[j].getDataType());

}

} /* iterating through the fields */

}

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Utils.java

import java.io.*;

import java.util.*;

import java.text.*;

/**

Miscelaneous functions required by the JavaDBF package.

*/

public final class Utils {

public static final int ALIGN_LEFT = 10;

public static final int ALIGN_RIGHT = 12;

private Utils(){}

public static int readLittleEndianInt( DataInput in)

throws IOException {

int bigEndian = 0;

for( int shiftBy=0; shiftBy<32; shiftBy+=8) {

bigEndian |= (in.readUnsignedByte()&0xff) << shiftBy;

}

return bigEndian;

}

public static short readLittleEndianShort( DataInput in)

throws IOException {

int low = in.readUnsignedByte() & 0xff;

int high = in.readUnsignedByte();

return (short )(high << 8 | low);

}

//add by viwo to read memo

public static int readHighEndianInt( DataInput in)

throws IOException {

int bigEndian = 0;

for( int shiftBy=24; shiftBy>-1; shiftBy-=8) {

bigEndian |= (in.readUnsignedByte()&0xff) << shiftBy;

}

return bigEndian;

}

//-------------------

public static byte[] trimLeftSpaces( byte [] arr) {

StringBuffer t_sb = new StringBuffer( arr.length);

for( int i=0; i<arr.length; i++) {

if( arr[i] != ' ') {

t_sb.append( (char)arr[ i]);

}

}

return t_sb.toString().getBytes();

}

public static short littleEndian( short value) {

short num1 = value;

short mask = (short)0xff;

short num2 = (short)(num1&mask);

num2<<=8;

mask<<=8;

num2 |= (num1&mask)>>8;

return num2;

}

public static int littleEndian(int value) {

int num1 = value;

int mask = 0xff;

int num2 = 0x00;

num2 |= num1 & mask;

for( int i=1; i<4; i++) {

num2<<=8;

mask <<= 8;

num2 |= (num1 & mask)>>(8*i);

}

return num2;

}

public static byte[] textPadding( String text, String characterSetName, int length) throws java.io.UnsupportedEncodingException {

return textPadding( text, characterSetName, length, Utils.ALIGN_LEFT);

}

public static byte[] textPadding( String text, String characterSetName, int length, int alignment) throws java.io.UnsupportedEncodingException {

return textPadding( text, characterSetName, length, alignment, (byte)' ');

}

public static byte[] textPadding( String text, String characterSetName, int length, int alignment,

byte paddingByte) throws java.io.UnsupportedEncodingException {

if( text.length() >= length) {

return text.substring( 0, length).getBytes( characterSetName);

}

byte byte_array[] = new byte[ length];

Arrays.fill( byte_array, paddingByte);

switch( alignment) {

case ALIGN_LEFT:

System.arraycopy( text.getBytes( characterSetName), 0, byte_array, 0, text.length());

break;

case ALIGN_RIGHT:

int t_offset = length - text.length();

System.arraycopy( text.getBytes( characterSetName), 0, byte_array, t_offset, text.length());

break;

}

return byte_array;

}

public static byte[] doubleFormating( Double doubleNum, String characterSetName, int fieldLength, int sizeDecimalPart) throws java.io.UnsupportedEncodingException{

int sizeWholePart = fieldLength - (sizeDecimalPart>0?( sizeDecimalPart + 1):0);

StringBuffer format = new StringBuffer( fieldLength);

for( int i=0; i<sizeWholePart; i++) {

format.append( "#");

}

if( sizeDecimalPart > 0) {

format.append( ".");

for( int i=0; i<sizeDecimalPart; i++) {

format.append( "0");

}

}

DecimalFormat df = new DecimalFormat( format.toString());

return textPadding( df.format( doubleNum.doubleValue()).toString(), characterSetName, fieldLength, ALIGN_RIGHT);

}

public static boolean contains( byte[] arr, byte value) {

boolean found = false;

for( int i=0; i<arr.length; i++) {

if( arr[i] == value) {

found = true;

break;

}

}

return found;

}

}

vssver.scc

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

DBConnection.java

import java.sql.Connection;

import java.sql.SQLException;

import java.util.Enumeration;

import java.util.ResourceBundle;

import javax.naming.InitialContext;

import javax.sql.DataSource;

import org.apache.log4j.Logger;

public class DBConnection {

static
Logger logger = (Logger)Logger.getLogger(DBConnection.class.getName());

DBConnection(){}

public static Connection getConnection() throws SQLException {

Connection conn = null;

DataSource ds = null;

String datasource = "";

//配置文件的路径和文件名

ResourceBundle bundle = ResourceBundle

.getBundle("com.ddit.serviceflat.config.jndi");

//读取配置文件

Enumeration temp = bundle.getKeys();

//键名

String tempKey = null;

//键值

String tempValue = null;

while (temp.hasMoreElements()) {

tempKey = (String) temp.nextElement();

tempValue = bundle.getString(tempKey);

//读取配置文件中键名为jndi的项所对应的值

if(tempKey.equals("webjndi")){

datasource = (String)tempValue.trim();

}

}

InitialContext ctx = null;

try {

ctx = new InitialContext();

ds = (DataSource) ctx.lookup(datasource);

conn = ds.getConnection();

} catch (Exception ex) {

logger.error(ex.getMessage());

}

return conn;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: