注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

午夜阳光

 
 
 

日志

 
 

java读写DBF文件(3)  

2012-02-12 15:58:47|  分类: java |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
     由于业务的需要,开发项目的时候,客户要求数据导入/导出,文件类类型为DBF文件,在网上找了一些相关源码,虽然可以完成DBF读写,但不足的是,导出文件时不支持中文字段名,字段名乱码。后经我对程序的修改,可支持中方字段名,字段名长度中5位,英文10,也可以中英文混用。下边将相关源码贴出来,供大家学习讨论,并附一读写简单例子,一共包括四个DBF读写类和一例子类。由于源码太长,不能一篇文章发完,将分成五部分。

          第一部分:DBFReader.java

          第二部分:DBFWriter.java

          第三部分:JDBField.java

          第四部分:JDBFException.java

          第五部分:我写的测试例子。  

          不明白的可以给我留言啊。

三 JDBField.java

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

// Referenced classes of package cn.edu.sut.oa.workadmin.sjcl:
//               JDBFException

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 == 'C' || type == 'N')
           {
               if(obj == null)
                   obj = "";
               if(obj instanceof String)
               {
                   String s = (String)obj;
                   if(s.length() > getLength())
                       throw new JDBFException("'" + obj + "' is longer than " + getLength() + " characters.");
                   StringBuffer stringbuffer = new StringBuffer(getLength() - s.length());
                   for(int i = 0; i < getLength() - s.length(); i++)
                       stringbuffer.append(' ');

                   return s + stringbuffer;
               } else
               {
                   throw new JDBFException("Expected a String, got " + obj.getClass() + ".");
               }
           }
           if(type == 'L')
           {
               if(obj == null)
                   obj = new Boolean(false);
               if(obj instanceof Boolean)
               {
                   Boolean boolean1 = (Boolean)obj;
                   return boolean1.booleanValue() ? "Y" : "N";
               } else
               {
                   throw new JDBFException("Expected a Boolean, got " + obj.getClass() + ".");
               }
           }
           if(type == 'D')
           {
               if(obj == null)
                   obj = new Date();
               if(obj instanceof Date)
               {
                   Date date = (Date)obj;
                   SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyyMMdd");
                   return simpledateformat.format(date);
               } else
               {
                   throw new JDBFException("Expected a Date, got " + obj.getClass() + ".");
               }
           } else
           {
               throw new JDBFException("Unrecognized JDBFField type: " + type);
           }
       }

       public Object parse(String s)
           throws JDBFException
       {
           s = s.trim();
           if(type == 'N' || type == 'F')
           {
               if(s.equals(""))
                   s = "0";
               try
               {
                   if(getDecimalCount() == 0)
                       return new Long(s);
                   else
                       return new Double(s);
               }
               catch(NumberFormatException numberformatexception)
               {
                   throw new JDBFException(numberformatexception);
               }
           }
           if(type == 'C')
               return s;
           if(type == 'L')
           {
               if(s.equals("Y") || s.equals("y") || s.equals("T") || s.equals("t"))
                   return new Boolean(true);
               if(s.equals("N") || s.equals("n") || s.equals("F") || s.equals("f"))
                   return new Boolean(false);
               else
                   throw new JDBFException("Unrecognized value for logical field: " + s);
           }
           if(type == 'D')
           {
               SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyyMMdd");
               try
               {
                   if("".equals(s))
                       return null;
                   else
                       return simpledateformat.parse(s);
               }
               catch(ParseException parseexception)
               {
                   throw new JDBFException(parseexception);
               }
           } else
           {
               throw new JDBFException("Unrecognized JDBFField type: " + type);
           }
       }

       public String toString()
       {
           return name;
       }

       private String name;
       private char type;
       private int length;
       private int decimalCount;
}

  评论这张
 
阅读(986)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018