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

午夜阳光

 
 
 

日志

 
 

java读写DBF文件  

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

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

         第一部分:DBFReader.java

         第二部分:DBFWriter.java

         第三部分:JDBField.java

         第四部分:JDBFException.java

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

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

一       DBFReader.java

import java.io.*;

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

public class DBFReader
{

        public DBFReader(String s)
            throws JDBFException
        {
            stream = null;
            fields = null;
            nextRecord = null;
            nFieldCount = 0;
            try
            {
                init(new FileInputStream(s));
            }
            catch(FileNotFoundException filenotfoundexception)
            {
                throw new JDBFException(filenotfoundexception);
            }
        }

        public DBFReader(InputStream inputstream)
            throws JDBFException
        {
            stream = null;
            fields = null;
            nextRecord = null;
            init(inputstream);
        }

        private void init(InputStream inputstream)
            throws JDBFException
        {
            try
            {
                stream = new DataInputStream(inputstream);
                int i = readHeader();
                fields = new JDBField[i];
                int j = 1;
                for(int k = 0; k < i; k++)
                {
                    fields[k] = readFieldHeader();
                    if(fields[k] != null)
                    {
                        nFieldCount++;
                        j += fields[k].getLength();
                    }
                }

                nextRecord = new byte[j];
                try
                {
                    stream.readFully(nextRecord);
                }
                catch(EOFException eofexception)
                {
                    nextRecord = null;
                    stream.close();
                }
                int l = 0;
                for(int i1 = 0; i1 < j; i1++)
                {
                 if(nextRecord==null)
                        break;
                 else
                 {
                        if(nextRecord[i1] != 32 && nextRecord[i1] != 42)
                            continue;
                        l = i1;
                        break;
                 }

                }

                if(l > 0)
                {
                    byte abyte0[] = new byte[l];
                    stream.readFully(abyte0);
                    for(int j1 = 0; j1 < j - l; j1++)
                        nextRecord[j1] = nextRecord[j1 + l];

                    for(int k1 = 0; k1 < l; k1++)
                        nextRecord[j - k1 - 1] = abyte0[l - k1 - 1];

                }
            }
            catch(IOException ioexception)
            {
                throw new JDBFException(ioexception);
            }
        }

        private int readHeader()
            throws IOException, JDBFException
        {
            byte abyte0[] = new byte[16];
            try
            {
                stream.readFully(abyte0);
            }
            catch(EOFException eofexception)
            {
                throw new JDBFException("Unexpected end of file reached.");
            }
            int i = abyte0[8];
            if(i < 0)
                i += 256;
            i += 256 * abyte0[9];
            i = --i / 32;
            i--;
            try
            {
                stream.readFully(abyte0);
            }
            catch(EOFException eofexception1)
            {
                throw new JDBFException("Unexpected end of file reached.");
            }
            return i;
        }

        private JDBField readFieldHeader()
            throws IOException, JDBFException
        {
            byte abyte0[] = new byte[16];
            try
            {
                stream.readFully(abyte0);
            }
            catch(EOFException eofexception)
            {
                throw new JDBFException("Unexpected end of file reached.");
            }
            if(abyte0[0] == 13 || abyte0[0] == 0)
            {
                stream.readFully(abyte0);
                return null;
            }
            StringBuffer stringbuffer = new StringBuffer(10);
            int i = 0;
            for(i = 0; i < 10; i++)
                if(abyte0[i] == 0)
                    break;

            stringbuffer.append(new String(abyte0, 0, i));
            char c = (char)abyte0[11];
            try
            {
                stream.readFully(abyte0);
            }
            catch(EOFException eofexception1)
            {
                throw new JDBFException("Unexpected end of file reached.");
            }
            int j = abyte0[0];
            int k = abyte0[1];
            if(j < 0)
                j += 256;
            if(k < 0)
                k += 256;
            return new JDBField(stringbuffer.toString(), c, j, k);
        }

        public int getFieldCount()
        {
            return nFieldCount;
        }

        public JDBField getField(int i)
        {
            return fields[i];
        }

        public boolean hasNextRecord()
        {
            return nextRecord != null;
        }

        public Object[] nextRecord()
            throws JDBFException
        {
            if(!hasNextRecord())
                throw new JDBFException("No more records available.");
            Object aobj[] = new Object[nFieldCount];
            int i = 1;
            for(int j = 0; j < aobj.length; j++)
            {
                int k = fields[j].getLength();
                StringBuffer stringbuffer = new StringBuffer(k);
                stringbuffer.append(new String(nextRecord, i, k));
                aobj[j] = fields[j].parse(stringbuffer.toString());
                i += fields[j].getLength();
            }

            try
            {
                stream.readFully(nextRecord);
            }
            catch(EOFException eofexception)
            {
                nextRecord = null;
            }
            catch(IOException ioexception)
            {
                throw new JDBFException(ioexception);
            }
            return aobj;
        }

        public String[] nextRecordString()
            throws JDBFException
        {
            if(!hasNextRecord())
                throw new JDBFException("No more records available.");
            String as[] = new String[nFieldCount];
            int i = 1;
            for(int j = 0; j < as.length; j++)
            {
                int k = fields[j].getLength();
                StringBuffer stringbuffer = new StringBuffer(k);
                stringbuffer.append(new String(nextRecord, i, k));
                as[j] = stringbuffer.toString();
                i += fields[j].getLength();
            }

            try
            {
                stream.readFully(nextRecord);
            }
            catch(EOFException eofexception)
            {
                nextRecord = null;
            }
            catch(IOException ioexception)
            {
                throw new JDBFException(ioexception);
            }
            return as;
        }

        public void close()
            throws JDBFException
        {
            nextRecord = null;
            try
            {
                stream.close();
            }
            catch(IOException ioexception)
            {
                throw new JDBFException(ioexception);
            }
        }

        private DataInputStream stream;
        private JDBField fields[];
        private byte nextRecord[];
        private int nFieldCount;
}

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

历史上的今天

评论

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

页脚

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