java 读取 dbf数据库的dbf文件代码
package cai.fu.com;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFReader;
import com.linuxense.javadbf.DBFWriter;
import net.sf.json.JSONObject;
import redis.clients.jedis.Jedis;
public class Rwdbf {
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)
{
InputStream fis = null;
Map map = new HashMap();
try {
// 读取文件的输入流
fis = new FileInputStream(path);
// 根据输入流初始化一个DBFReader实例,用来读取DBF文件信息
DBFReader reader = new DBFReader(fis);
// 调用DBFReader对实例方法得到path文件中字段的个数
int fieldsCount = reader.getFieldCount();
Object[] rowValues;
// 一条条取出path文件中记录
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 pricetop = topPrices - 9;
double pricefall = fallPrices - 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();
// 7.跌停价
map.put("pricefall", pricefa);
// 6.涨停价
map.put("pricetop", pricetop);
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) {
// 2.今开
map.put("open", rowValues[i].toString());
} else if (i == 4) {
// 1.现价
map.put("currentPrice", rowValues[i].toString());
} else if (i == 5) {
// 9.成交数量
map.put("amount", new BigDecimal(rowValues[i].toString()).toPlainString());
} else if (i == 8) {
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 == 9) {
BigDecimal hazdcj = new BigDecimal(rowValues[i].toString());
double priceHazdcj = hazdcj.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
// 5.最低价
map.put("low", priceHazdcj);
} else if (i == 15) {
// HQSJW5:行情Sell价位5
// HQSSL5:行情Buy数量5
map.put("HQSJW5", rowValues[i].toString());
} else if (i == 16) {
map.put("HQSSL5", rowValues[i].toString());
} else if (i == 17) {
map.put("HQSJW4", rowValues[i].toString());
} else if (i == 18) {
map.put("HQSSL4", rowValues[i].toString());
} else if (i == 19) {
map.put("HQSJW3", rowValues[i].toString());
} else if (i == 20) {
map.put("HQSSL3", rowValues[i].toString());
} else if (i == 21) {
map.put("HQSJW2", rowValues[i].toString());
} else if (i == 22) {
map.put("HQSSL2", rowValues[i].toString());
} else if (i == 23) {
map.put("HQSJW1", rowValues[i].toString());
} else if (i == 24) {
map.put("HQSSL1", rowValues[i].toString());
} else if (i == 25) {
map.put("HQBJW1", rowValues[i].toString());
} else if (i == 26) {
map.put("HQBSL1", rowValues[i].toString());
} else if (i == 27) {
map.put("HQBJW2", rowValues[i].toString());
} else if (i == 28) {
map.put("HQBSL2", rowValues[i].toString());
} else if (i == 29) {
map.put("HQBJW3", rowValues[i].toString());
} else if (i == 30) {
map.put("HQBSL3", rowValues[i].toString());
} else if (i == 31) {
map.put("HQBJW4", rowValues[i].toString());
} else if (i == 32) {
map.put("HQBSL4", rowValues[i].toString());
} else if (i == 33) {
map.put("HQBJW5", rowValues[i].toString());
} else if (i == 34) {
map.put("HQBSL5", 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) {
Rwdbf rdbf = new Rwdbf();
String path = "E:\\SJSHQ.DBF";
setup();
rdbf.readDBF(path);
}
}