java读取dbf乱码_javadbf 实现解决中文乱码问题 | 学步园

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") !&

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值