java 读取dbf 文件 dbf含有 num.format -.--- 不能解析时,用这个

来源:互联网 发布:拼音发音软件下载 编辑:程序博客网 时间:2024/05/21 06:23

1.

 

public abstract class DBFBase {
 protected String characterSetName = "8859_1";
 protected final int END_OF_DATA = 26;

 public String getCharactersetName() {
  return this.characterSetName;
 }

 public void setCharactersetName(String characterSetName) {
  this.characterSetName = characterSetName;
 }
}

 

 

2.

 

import java.io.IOException;

public class DBFException extends IOException {
 public DBFException() {
 }

 public DBFException(String msg) {
  super(msg);
 }
}

 

3.

 

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class DBFField {
 public static final byte FIELD_TYPE_C = 67;
 public static final byte FIELD_TYPE_L = 76;
 public static final byte FIELD_TYPE_N = 78;
 public static final byte FIELD_TYPE_F = 70;
 public static final byte FIELD_TYPE_D = 68;
 public static final byte FIELD_TYPE_M = 77;
 byte[] fieldName = new byte[11];
 byte dataType;
 int reserv1;
 int fieldLength;
 byte decimalCount;
 short reserv2;
 byte workAreaId;
 short reserv3;
 byte setFieldsFlag;
 byte[] reserv4 = new byte[7];
 byte indexFieldFlag;
 int nameNullIndex = 0;

 protected static DBFField createField(DataInput in) throws IOException {
  DBFField field = new DBFField();

  byte t_byte = in.readByte();
  if (t_byte == 13) {
   return null;
  }

  in.readFully(field.fieldName, 1, 10);
  field.fieldName[0] = t_byte;

  for (int i = 0; i < field.fieldName.length; i++) {
   if (field.fieldName[i] != 0)
    continue;
   field.nameNullIndex = i;
   break;
  }

  field.dataType = in.readByte();
  field.reserv1 = Utils.readLittleEndianInt(in);
  field.fieldLength = in.readUnsignedByte();
  field.decimalCount = in.readByte();
  field.reserv2 = Utils.readLittleEndianShort(in);
  field.workAreaId = in.readByte();
  field.reserv2 = Utils.readLittleEndianShort(in);
  field.setFieldsFlag = in.readByte();
  in.readFully(field.reserv4);
  field.indexFieldFlag = in.readByte();

  return field;
 }

 protected void write(DataOutput out) throws IOException {
  out.write(this.fieldName);
  out.write(new byte[11 - this.fieldName.length]);

  out.writeByte(this.dataType);
  out.writeInt(0);
  out.writeByte(this.fieldLength);
  out.writeByte(this.decimalCount);
  out.writeShort(0);
  out.writeByte(0);
  out.writeShort(0);
  out.writeByte(0);
  out.write(new byte[7]);
  out.writeByte(0);
 }

 public String getName() {
  return new String(this.fieldName, 0, this.nameNullIndex);
 }

 public byte getDataType() {
  return this.dataType;
 }

 public int getFieldLength() {
  return this.fieldLength;
 }

 public int getDecimalCount() {
  return this.decimalCount;
 }

 /** @deprecated */
 public void setFieldName(String value) {
  setName(value);
 }

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

 public void setDataType(byte value) {
  switch (value) {
  case 68:
   this.fieldLength = 8;
  case 67:
  case 70:
  case 76:
  case 77:
  case 78:
   this.dataType = value;
   break;
  case 69:
  case 71:
  case 72:
  case 73:
  case 74:
  case 75:
  default:
   throw new IllegalArgumentException("Unknown data type");
  }
 }

 public void setFieldLength(int value) {
  if (value <= 0) {
   throw new IllegalArgumentException(
     "Field length should be a positive number");
  }

  if (this.dataType == 68) {
   throw new UnsupportedOperationException(
     "Cannot do this on a Date field");
  }

  this.fieldLength = value;
 }

 public void setDecimalCount(int value) {
  if (value < 0) {
   throw new IllegalArgumentException(
     "Decimal length should be a positive number");
  }

  if (value > this.fieldLength) {
   throw new IllegalArgumentException(
     "Decimal length should be less than field length");
  }

  this.decimalCount = (byte) value;
 }
}

 

4.

 

 

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.GregorianCalendar;
import java.util.Vector;

class DBFHeader
{
  static final byte SIG_DBASE_III = 3;
  byte signature;
  byte year;
  byte month;
  byte day;
  int numberOfRecords;
  short headerLength;
  short recordLength;
  short reserv1;
  byte incompleteTransaction;
  byte encryptionFlag;
  int freeRecordThread;
  int reserv2;
  int reserv3;
  byte mdxFlag;
  byte languageDriver;
  short reserv4;
  DBFField[] fieldArray;
  byte terminator1;

  DBFHeader()
  {
    this.signature = 3;
    this.terminator1 = 13;
  }

  void read(DataInput dataInput) throws IOException
  {
    this.signature = dataInput.readByte();
    this.year = dataInput.readByte();
    this.month = dataInput.readByte();
    this.day = dataInput.readByte();
    this.numberOfRecords = Utils.readLittleEndianInt(dataInput);

    this.headerLength = Utils.readLittleEndianShort(dataInput);
    this.recordLength = Utils.readLittleEndianShort(dataInput);

    this.reserv1 = Utils.readLittleEndianShort(dataInput);
    this.incompleteTransaction = dataInput.readByte();
    this.encryptionFlag = dataInput.readByte();
    this.freeRecordThread = Utils.readLittleEndianInt(dataInput);
    this.reserv2 = dataInput.readInt();
    this.reserv3 = dataInput.readInt();
    this.mdxFlag = dataInput.readByte();
    this.languageDriver = dataInput.readByte();
    this.reserv4 = Utils.readLittleEndianShort(dataInput);

    Vector v_fields = new Vector();

    DBFField field = DBFField.createField(dataInput);
    while (field != null)
    {
      v_fields.addElement(field);
      field = DBFField.createField(dataInput);
    }

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

    for (int i = 0; i < this.fieldArray.length; i++)
    {
      this.fieldArray[i] = ((DBFField)v_fields.elementAt(i));
    }
  }

  void write(DataOutput dataOutput)
    throws IOException
  {
    dataOutput.writeByte(this.signature);

    GregorianCalendar calendar = new GregorianCalendar();
    this.year = (byte)(calendar.get(1) - 1900);
    this.month = (byte)(calendar.get(2) + 1);
    this.day = (byte)calendar.get(5);

    dataOutput.writeByte(this.year);
    dataOutput.writeByte(this.month);
    dataOutput.writeByte(this.day);

    this.numberOfRecords = Utils.littleEndian(this.numberOfRecords);
    dataOutput.writeInt(this.numberOfRecords);

    this.headerLength = findHeaderLength();
    dataOutput.writeShort(Utils.littleEndian(this.headerLength));

    this.recordLength = findRecordLength();
    dataOutput.writeShort(Utils.littleEndian(this.recordLength));

    dataOutput.writeShort(Utils.littleEndian(this.reserv1));
    dataOutput.writeByte(this.incompleteTransaction);
    dataOutput.writeByte(this.encryptionFlag);
    dataOutput.writeInt(Utils.littleEndian(this.freeRecordThread));
    dataOutput.writeInt(Utils.littleEndian(this.reserv2));
    dataOutput.writeInt(Utils.littleEndian(this.reserv3));

    dataOutput.writeByte(this.mdxFlag);
    dataOutput.writeByte(this.languageDriver);
    dataOutput.writeShort(Utils.littleEndian(this.reserv4));

    for (int i = 0; i < this.fieldArray.length; i++)
    {
      this.fieldArray[i].write(dataOutput);
    }

    dataOutput.writeByte(this.terminator1);
  }

  private short findHeaderLength()
  {
    return (short)(32 + 32 * this.fieldArray.length + 1);
  }

  private short findRecordLength()
  {
    int recordLength = 0;
    for (int i = 0; i < this.fieldArray.length; i++)
    {
      recordLength += this.fieldArray[i].getFieldLength();
    }

    return (short)(recordLength + 1);
  }
}

 

5.

 

import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.util.GregorianCalendar;

public class DBFReader extends DBFBase {
 DataInputStream dataInputStream;
 DBFHeader header;
 boolean isClosed = true;

 public DBFReader(InputStream in) throws DBFException {
  try {
   this.dataInputStream = new DataInputStream(in);
   this.isClosed = false;
   this.header = new DBFHeader();
   this.header.read(this.dataInputStream);

   int t_dataStartIndex = this.header.headerLength
     - (32 + 32 * this.header.fieldArray.length) - 1;
   if (t_dataStartIndex > 0) {
    this.dataInputStream.skip(t_dataStartIndex);
   }
  } catch (IOException e) {
   throw new DBFException(e.getMessage());
  }
 }

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

 public int getRecordCount() {
  return this.header.numberOfRecords;
 }

 public DBFField getField(int index) throws DBFException {
  if (this.isClosed) {
   throw new DBFException("Source is not open");
  }

  return this.header.fieldArray[index];
 }

 public int getFieldCount() throws DBFException {
  if (this.isClosed) {
   throw new DBFException("Source is not open");
  }

  if (this.header.fieldArray != null) {
   return this.header.fieldArray.length;
  }

  return -1;
 }

 public Object[] nextRecord() throws DBFException {
  if (this.isClosed) {
   throw new DBFException("Source is not open");
  }

  Object[] recordObjects = new Object[this.header.fieldArray.length];
  try {
   boolean isDeleted = false;
   do {
    if (isDeleted) {
     this.dataInputStream.skip(this.header.recordLength - 1);
    }

    int t_byte = this.dataInputStream.readByte();
    if (t_byte == 26) {
     return null;
    }

    isDeleted = t_byte == 42;
   } while (isDeleted);

   for (int i = 0; i < this.header.fieldArray.length; i++) {
    switch (this.header.fieldArray[i].getDataType()) {
    case 67:
     byte[] b_array = new byte[this.header.fieldArray[i]
       .getFieldLength()];
     this.dataInputStream.read(b_array);
     recordObjects[i] = new String(b_array,
       this.characterSetName);
     break;
    case 68:
     byte[] t_byte_year = new byte[4];
     this.dataInputStream.read(t_byte_year);

     byte[] t_byte_month = new byte[2];
     this.dataInputStream.read(t_byte_month);

     byte[] t_byte_day = new byte[2];
     this.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();
     } catch (NumberFormatException e) {
      recordObjects[i] = null;
     }

     break;
    case 70:
     try {
      byte[] t_float = new byte[this.header.fieldArray[i]
        .getFieldLength()];
      this.dataInputStream.read(t_float);
      t_float = Utils.trimLeftSpaces(t_float);
      if ((t_float.length > 0)
        && (!Utils.contains(t_float, (byte) 63))) {
       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 78:
     try {
      byte[] t_numeric = new byte[this.header.fieldArray[i]
        .getFieldLength()];
      this.dataInputStream.read(t_numeric);
      t_numeric = Utils.trimLeftSpaces(t_numeric);
      // 娣诲姞浠g爜
      String strtemp = new String(t_numeric);
      if (strtemp.equals("-.---") || strtemp.equals("-")) {
       recordObjects[i] = 0.0;// 杩欒浠g爜锛屽湪浣犲彧闇�瑕佽緭鍑洪噷闈㈢殑鏁版嵁鏃讹紝鍙互涓嶇敤鍔狅紝涓嶄細鍑洪敊銆傚綋浣犺璇荤敤杩欓噷闈㈢殑鏁版嵁鏃跺氨蹇呴』鍔犮��
       continue;
      }
      // 缁撴潫浠g爜
      if ((t_numeric.length > 0)
        && (!Utils.contains(t_numeric, (byte) 63))) {
       recordObjects[i] = new Double(new String(t_numeric));
      } else {
       recordObjects[i] = null;
      }
     } catch (NumberFormatException e) {
      throw new DBFException("Failed to parse Number: "
        + e.getMessage());
     }

     break;
    case 76:
     byte t_logical = this.dataInputStream.readByte();
     if ((t_logical == 89) || (t_logical == 116)
       || (t_logical == 84) || (t_logical == 116)) {
      recordObjects[i] = Boolean.TRUE;
     } else {
      recordObjects[i] = Boolean.FALSE;
     }
     break;
    case 77:
     recordObjects[i] = new String("null");
     break;
    case 69:
    case 71:
    case 72:
    case 73:
    case 74:
    case 75:
    default:
     recordObjects[i] = new String("null");
    }
   }
  } catch (EOFException e) {
   return null;
  } catch (IOException e) {
   throw new DBFException(e.getMessage());
  }

  return recordObjects;
 }
}

 

6.

 

import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Vector;

public class DBFWriter extends DBFBase {
 DBFHeader header;
 Vector v_records = new Vector();
 int recordCount = 0;
 RandomAccessFile raf = null;
 boolean appendMode = false;
 private String jdField_characterSetName_of_type_JavaLangString;

 public DBFWriter() {
  this.header = new DBFHeader();
 }

 public DBFWriter(File dbfFile) throws DBFException {
  try {
   this.raf = new RandomAccessFile(dbfFile, "rw");

   if ((!dbfFile.exists()) || (dbfFile.length() == 0L)) {
    this.header = new DBFHeader();
    return;
   }

   this.header = new DBFHeader();
   this.header.read(this.raf);

   this.raf.seek(this.raf.length() - 1L);
  } 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;
 }

 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)
    continue;
   throw new DBFException("Field " + (i + 1) + " is null");
  }

  this.header.fieldArray = fields;
  try {
   if ((this.raf != null) && (this.raf.length() == 0L)) {
    this.header.write(this.raf);
   }
  } catch (IOException e) {
   throw new DBFException("Error accesing file");
  }
 }

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

   switch (this.header.fieldArray[i].getDataType()) {
   case 67:
    if ((values[i] instanceof String))
     continue;
    throw new DBFException("Invalid value for field " + i);
   case 76:
    if ((values[i] instanceof Boolean))
     continue;
    throw new DBFException("Invalid value for field " + i);
   case 78:
    if ((values[i] instanceof Double))
     continue;
    throw new DBFException("Invalid value for field " + i);
   case 68:
    if ((values[i] instanceof Date))
     continue;
    throw new DBFException("Invalid value for field " + i);
   case 70:
    if ((values[i] instanceof Double))
     continue;
    throw new DBFException("Invalid value for field " + i);
   case 69:
   case 71:
   case 72:
   case 73:
   case 74:
   case 75:
   case 77:
   }
  }
  if (this.raf == null) {
   this.v_records.addElement(values);
  } else {
   try {
    writeRecord(this.raf, values);
    this.recordCount += 1;
   } catch (IOException e) {
    throw new DBFException("Error occured while writing record. "
      + e.getMessage());
   }
  }
 }

 public void write(OutputStream out) throws DBFException {
  try {
   if (this.raf == null) {
    DataOutputStream outStream = new DataOutputStream(out);

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

    int t_recCount = this.v_records.size();
    for (int i = 0; i < t_recCount; i++) {
     Object[] t_values = (Object[]) this.v_records.elementAt(i);

     writeRecord(outStream, t_values);
    }

    outStream.write(26);
    outStream.flush();
   } else {
    this.header.numberOfRecords = this.recordCount;
    this.raf.seek(0L);
    this.header.write(this.raf);
    this.raf.seek(this.raf.length());
    this.raf.writeByte(26);
    this.raf.close();
   }

  } catch (IOException e) {
   throw new DBFException(e.getMessage());
  }
 }

 public void write() throws DBFException {
  write(null);
 }

 private void writeRecord(DataOutput dataOutput, Object[] objectArray)
   throws IOException {
  dataOutput.write(32);
  for (int j = 0; j < this.header.fieldArray.length; j++) {
   switch (this.header.fieldArray[j].getDataType()) {
   case 67:
    if (objectArray[j] != null) {
     String str_value = objectArray[j].toString();
     dataOutput
       .write(Utils
         .textPadding(
           str_value,
           this.jdField_characterSetName_of_type_JavaLangString,
           this.header.fieldArray[j]
             .getFieldLength()));
    } else {
     dataOutput
       .write(Utils
         .textPadding(
           "",
           this.jdField_characterSetName_of_type_JavaLangString,
           this.header.fieldArray[j]
             .getFieldLength()));
    }

    break;
   case 68:
    if (objectArray[j] != null) {
     GregorianCalendar calendar = new GregorianCalendar();
     calendar.setTime((Date) objectArray[j]);
     StringBuffer t_sb = new StringBuffer();
     dataOutput
       .write(String.valueOf(calendar.get(1)).getBytes());
     dataOutput
       .write(Utils.textPadding(
         String.valueOf(calendar.get(2) + 1),
         this.jdField_characterSetName_of_type_JavaLangString,
         2, 12, (byte) 48));
     dataOutput
       .write(Utils.textPadding(
         String.valueOf(calendar.get(5)),
         this.jdField_characterSetName_of_type_JavaLangString,
         2, 12, (byte) 48));
    } else {
     dataOutput.write("        ".getBytes());
    }

    break;
   case 70:
    if (objectArray[j] != null) {
     dataOutput
       .write(Utils
         .doubleFormating(
           (Double) objectArray[j],
           this.jdField_characterSetName_of_type_JavaLangString,
           this.header.fieldArray[j]
             .getFieldLength(),
           this.header.fieldArray[j]
             .getDecimalCount()));
    } else {
     dataOutput
       .write(Utils
         .textPadding(
           "?",
           this.jdField_characterSetName_of_type_JavaLangString,
           this.header.fieldArray[j]
             .getFieldLength(), 12));
    }

    break;
   case 78:
    if (objectArray[j] != null) {
     dataOutput
       .write(Utils
         .doubleFormating(
           (Double) objectArray[j],
           this.jdField_characterSetName_of_type_JavaLangString,
           this.header.fieldArray[j]
             .getFieldLength(),
           this.header.fieldArray[j]
             .getDecimalCount()));
    } else {
     dataOutput
       .write(Utils
         .textPadding(
           "?",
           this.jdField_characterSetName_of_type_JavaLangString,
           this.header.fieldArray[j]
             .getFieldLength(), 12));
    }

    break;
   case 76:
    if (objectArray[j] != null) {
     if ((Boolean) objectArray[j] == Boolean.TRUE) {
      dataOutput.write(84);
     } else {
      dataOutput.write(70);
     }
    } else {
     dataOutput.write(63);
    }

    break;
   case 77:
    break;
   case 69:
   case 71:
   case 72:
   case 73:
   case 74:
   case 75:
   default:
    throw new DBFException("Unknown field type "
      + this.header.fieldArray[j].getDataType());
   }
  }
 }
}

 

7.

 


import java.io.FileInputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;
import redis.clients.jedis.Jedis;

/**
 *
 *
 * @author tj
 *
 */
public class Rwdbf {

 /**
  *
  *
  * @param path
  */
 private static Jedis jedis;

 public static void setup() {
  // 连接redis服务器,192.168.0.188:6379
  jedis = new Jedis("192.168.0.188", 6379);
  // 权限认证
  // jedis.auth("sa123456");
 }

 public static void readDBF(String path) {
  setup();

  InputStream fis = null;
  Map map = new HashMap();
  try {
   fis = new FileInputStream(path);
   DBFReader reader = new DBFReader(fis);
   reader.setCharactersetName("GBK");
   int fieldsCount = reader.getFieldCount();
   Object[] rowValues;
   while ((rowValues = reader.nextRecord()) != null) {
    for (int i = 0; i < rowValues.length; i++) {
     if (i == 0) {
      // 0.
      map.put("symbol", rowValues[i].toString());
     } else if (i == 2) {
      String cloPrices = rowValues[i].toString();
      double topPrices = Double.parseDouble(cloPrices) * 1.1;
      double fallPrices = Double.parseDouble(cloPrices) * 0.90;
      double pricefall = fallPrices - 9;
      double pricetop = topPrices - 9;

      BigDecimal bfall = new BigDecimal(pricefall);
      double pricefa = bfall.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
      BigDecimal btop = new BigDecimal(pricetop);
      double priceto = btop.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
      // 6.涨停价
      map.put("pricetop", pricetop);
      // 7.跌停价
      map.put("pricefall", pricefa);
      BigDecimal bdto = new BigDecimal(Double.parseDouble(rowValues[i].toString()));
      double pricebdto = bdto.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
      // 3.收盘价
      map.put("close", pricebdto);
     } else if (i == 3) {
      BigDecimal hqzgcj = new BigDecimal(rowValues[i].toString());
      double pricehqzgcj = hqzgcj.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
      // 2.开盘价
      map.put("open", pricehqzgcj);
     } else if (i == 5) {
      BigDecimal hqzgcj = new BigDecimal(rowValues[i].toString());
      double pricehqzgcj = hqzgcj.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
      // 4.最高价格
      map.put("high", pricehqzgcj);
     } else if (i == 6) {
      BigDecimal hqzgcj = new BigDecimal(rowValues[i].toString());
      double pricehqzgcj = hqzgcj.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
      // 5.最低价格
      map.put("low", pricehqzgcj);
     } else if (i == 7) {
      BigDecimal hqzgcj = new BigDecimal(rowValues[i].toString());
      double pricehqzgcj = hqzgcj.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
      // 1.现价
      map.put("currentPrice", pricehqzgcj);
     } else if (i == 8) {
      // 买1
      map.put("HQBJW1", rowValues[i].toString());
     } else if (i == 9) {
      // 卖1
      map.put("HQSJW1", rowValues[i].toString());
     } else if (i == 10) {
      // 9.成交数量
      
    
      
      map.put("amount", new BigDecimal(rowValues[i].toString()).toPlainString());
     } else if (i == 12) {
      // 买数量1
      map.put("HQBSL1", rowValues[i].toString());
     } else if (i == 13) {
      // 买价位2
      map.put("HQBJW2", rowValues[i].toString());
     } else if (i == 14) {
      // 买数量2
      map.put("HQBSL2", rowValues[i].toString());
     } else if (i == 15) {
      // 买价位3
      map.put("HQBJW3", rowValues[i].toString());
     } else if (i == 16) {
      // 买数量3
      map.put("HQBSL3", rowValues[i].toString());
     } else if (i == 17) {
      // 卖数量1
      map.put("HQSSL1", rowValues[i].toString());
     } else if (i == 18) {
      // 卖价位2
      map.put("HQSJW2", rowValues[i].toString());
     } else if (i == 19) {
      // 卖数量2
      map.put("HQSSL2", rowValues[i].toString());
     } else if (i == 20) {
      // 卖价位3
      map.put("HQSJW2", rowValues[i].toString());
     } else if (i == 21) {
      // 卖数量3
      map.put("HQSSL2", rowValues[i].toString());
     } else if (i == 22) {
      // 买价位4
      map.put("HQBJW4", rowValues[i].toString());
     } else if (i == 23) {
      // 买数量4
      map.put("HQBSL4", rowValues[i].toString());
     } else if (i == 24) {
      // 买价位5
      map.put("HQBJW5", rowValues[i].toString());
     } else if (i == 25) {
      // 买数量5
      map.put("HQBSL5", rowValues[i].toString());
     } else if (i == 26) {
      // 卖价位4
      map.put("HQSJW4", rowValues[i].toString());
     } else if (i == 27) {
      // 卖数量4
      map.put("HQSSL4", rowValues[i].toString());
     } else if (i == 28) {
      // 卖价位5
      map.put("HQSJW5", rowValues[i].toString());
     } else if (i == 29) {
      // 卖数量5
      map.put("HQSSL5", rowValues[i].toString());
     }
     // 8.换手率
     map.put("change", "");
    }
    JSONObject jsonObject = JSONObject.fromObject(map);
    jedis.set("5_" + jsonObject.get("symbol") + "", jsonObject.toString());
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   try {
    fis.close();
   } catch (Exception e) {
   }
  }
 }

 public static void main(String[] args) {

  String path = "E:\\SHOW2003.DBF";

  readDBF(path);

 }

}

 

8.

 

 

import java.io.DataInput;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.util.Arrays;

public final class Utils {
 public static final int ALIGN_LEFT = 10;
 public static final int ALIGN_RIGHT = 12;

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

 public static byte[] trimLeftSpaces(byte[] arr) {
  StringBuffer t_sb = new StringBuffer(arr.length);

  for (int i = 0; i < arr.length; i++) {
   if (arr[i] == 32)
    continue;
   t_sb.append((char) arr[i]);
  }

  return t_sb.toString().getBytes();
 }

 public static short littleEndian(short value) {
  short num1 = value;
  short mask = 255;

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

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

  return num2;
 }

 public static int littleEndian(int value) {
  int num1 = value;
  int mask = 255;
  int num2 = 0;

  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 UnsupportedEncodingException {
  return textPadding(text, characterSetName, length, 10);
 }

 public static byte[] textPadding(String text, String characterSetName,
   int length, int alignment) throws UnsupportedEncodingException {
  return textPadding(text, characterSetName, length, alignment, (byte) 32);
 }

 public static byte[] textPadding(String text, String characterSetName,
   int length, int alignment, byte paddingByte)
   throws 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 10:
   System.arraycopy(text.getBytes(characterSetName), 0, byte_array, 0,
     text.length());
   break;
  case 12:
   int t_offset = length - text.length();
   System.arraycopy(text.getBytes(characterSetName), 0, byte_array,
     t_offset, text.length());
  }

  return byte_array;
 }

 public static byte[] doubleFormating(Double doubleNum,
   String characterSetName, int fieldLength, int sizeDecimalPart)
   throws 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, 12);
 }

 public static boolean contains(byte[] arr, byte value) {
  boolean found = false;
  for (int i = 0; i < arr.length; i++) {
   if (arr[i] != value)
    continue;
   found = true;
   break;
  }

  return found;
 }
}

 

 

 

 

0 0
原创粉丝点击