java连接dbf_java操作DBF文件源文件

1.JDBField.java

package com.svcon.jdbf;

import java.text.*;

import java.util.Date;

public class JDBField

{

public JDBField(String s, char c, int i, int j)

throws JDBFException

{

if(s.length() > 10)

throw new JDBFException("The field name is more than 10 characters long: " + s);

if(c != 'C' && c != 'N' && c != 'L' && c != 'D' && c != 'F')

throw new JDBFException("The field type is not a valid. Got: " + c);

if(i < 1)

throw new JDBFException("The field length should be a positive integer. Got: " + i);

if(c == 'C' && i >= 254)

throw new JDBFException("The field length should be less than 254 characters for character fields. Got: " + i);

if(c == 'N' && i >= 21)

throw new JDBFException("The field length should be less than 21 digits for numeric fields. Got: " + i);

if(c == 'L' && i != 1)

throw new JDBFException("The field length should be 1 characater for logical fields. Got: " + i);

if(c == 'D' && i != 8)

throw new JDBFException("The field length should be 8 characaters for date fields. Got: " + i);

if(c == 'F' && i >= 21)

throw new JDBFException("The field length should be less than 21 digits for floating point fields. Got: " + i);

if(j < 0)

throw new JDBFException("The field decimal count should not be a negative integer. Got: " + j);

if((c == 'C' || c == 'L' || c == 'D') && j != 0)

throw new JDBFException("The field decimal count should be 0 for character, logical, and date fields. Got: " + j);

if(j > i - 1)

{

throw new JDBFException("The field decimal count should be less than the length - 1. Got: " + j);

} else

{

name = s;

type = c;

length = i;

decimalCount = j;

return;

}

}

public String getName()

{

return name;

}

public char getType()

{

return type;

}

public int getLength()

{

return length;

}

public int getDecimalCount()

{

return decimalCount;

}

public String format(Object obj)

throws JDBFException

{

if(type == 'N' || type == 'F')

{

if(obj == null)

obj = new Double(0.0D);

if(obj instanceof Number)

{

Number number = (Number)obj;

StringBuffer stringbuffer = new StringBuffer(getLength());

for(int i = 0; i < getLength(); i++)

stringbuffer.append("#");

if(getDecimalCount() > 0)

stringbuffer.setCharAt(getLength() - getDecimalCount() - 1, '.');

DecimalFormat decimalformat = new DecimalFormat(stringbuffer.toString());

String s1 = decimalformat.format(number);

int k = getLength() - s1.length();

if(k < 0)

throw new JDBFException("Value " + number + " cannot fit in pattern: '" + stringbuffer + "'.");

StringBuffer stringbuffer2 = new StringBuffer(k);

for(int l = 0; l < k; l++)

stringbuffer2.append(" ");

return stringbuffer2 + s1;

} else

{

throw new JDBFException("Expected a Number, got " + obj.getClass() + ".");

}

}

if(type == 'C')

{

if(obj == null)

obj = "";

if(obj instanceof String)

{

String s = (String)obj;

if(s.length() > getLength())

throw new JDBFException(&

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值