默认头像
路人甲
路人甲
  • 注册日期2003-10-18
  • 发帖数118
  • QQ
  • 铜币400枚
  • 威望0点
  • 贡献值0点
  • 银元0个
阅读:2905回复:4

我重写的读dbf的代码( MyDbf.java)(geotools)

楼主#
更多 发布于:2005-05-21 19:03

geotools读中文字段会出错,是因为它把一个记录做为一个StringBuffer处理的,中文两个Byte合成了一个字,所以合成StringBuffer之后再找位置就出错了。解决办法是把一个记录做为一个StringBuffer的数组,取值的时候按数组内的顺序查找位置

其实继承可以少写点代码,但是。。。。。看了原来的代码实在生气,其实大部分代码还是 copy的。

可能用到了我改写的其它类,也可能没有,不记得了。要有的话改成引用原来的应该可以编译通过。

package uk.ac.leeds.ccg.dbffile;

import cmp.LEDataStream.LEDataInputStream; import java.io.*; import java.net.URL; import java.net.URLConnection; import java.util.Vector;

public class MyDbf     implements DbfConsts {     class DbfFileHeader     {         private void getDbfFileHeader(LEDataInputStream file)             throws IOException         {             dbf_id = file.readUnsignedByte();             if(dbf_id == 3)                 hasmemo = true;             else                 hasmemo = false;             last_update_y = file.readUnsignedByte();             last_update_m = file.readUnsignedByte();             last_update_d = file.readUnsignedByte();             last_rec = file.readInt();             data_offset = file.readShort();             rec_size = file.readShort();             filesize = rec_size * last_rec + data_offset + 1;             numfields = (data_offset - 32 - 1) / 32;             file.skipBytes(20);         }

       public DbfFileHeader(LEDataInputStream file)             throws IOException         {             getDbfFileHeader(file);         }     }          class Record     {      private int numfields;      private StringBuffer[] record;           public Record(int field_count)      {       numfields = field_count;       record = new StringBuffer[field_count];      }           public void addData(StringBuffer data, int seq)      {       if (seq < numfields)        record[seq] = data;       else        System.out.println("Dbf->Record->Error!!! Out of numfield boundary!!!");      }           public StringBuffer getData(int seq)      {       if(seq < numfields)        return record[seq];       else       {        System.out.println("Dbf->Record->Error!!! Data not found!!!");        return new StringBuffer("");       }             }     }          static final boolean DEBUG = false;     static final String DBC = "Dbf->";     int dbf_id;     int last_update_d;     int last_update_m;     int last_update_y;     int last_rec;     int data_offset;     int rec_size;     Record records[];     int position;     boolean hasmemo;     boolean isFile;     RandomAccessFile rFile;     LEDataInputStream dFile;     int filesize;     int numfields;     public DbfFieldDef fielddef[];

   public MyDbf(URL url)         throws DbfFileException, IOException     {         position = 0;         isFile = false;         URLConnection uc = url.openConnection();         InputStream in = uc.getInputStream();         LEDataInputStream sfile = new LEDataInputStream(in);         init(sfile);     }

   public MyDbf(InputStream in)         throws DbfFileException, IOException     {         position = 0;         isFile = false;         LEDataInputStream sfile = new LEDataInputStream(in);         init(sfile);     }

   public MyDbf(String name)         throws DbfFileException, IOException     {         position = 0;         isFile = false;         URL url = new URL(name);         URLConnection uc = url.openConnection();         InputStream in = uc.getInputStream();         LEDataInputStream sfile = new LEDataInputStream(in);         init(sfile);     }

   public MyDbf(File file)         throws DbfFileException, IOException     {         position = 0;         isFile = false;         InputStream in = new FileInputStream(file);         LEDataInputStream sfile = new LEDataInputStream(in);         rFile = new RandomAccessFile(file, "r");         isFile = true;         init(sfile);     }

   public String getLastUpdate()     {         String date = String.valueOf((new StringBuffer(String.valueOf(last_update_d))).append("/").append(last_update_m + 1).append("/").append(1900 + last_update_y));         return date;     }

   public int getLastRec()     {         return last_rec;     }

   public int getRecSize()     {         return rec_size;     }

   public int getNumFields()     {         return numfields;     }

   public int getFieldNumber(String name)     {         for(int i = 0; i < numfields; i++)             if(name.equalsIgnoreCase(fielddef.fieldname.toString()))                 return i;

       return -1;     }

   public int getFileSize()     {         return filesize;     }

   public StringBuffer getFieldName(int col)     {         if(col >= numfields)             throw new IllegalArgumentException("Dbf->column number specified is invalid. It's higher than the amount of columns available ".concat(String.valueOf(String.valueOf(numfields))));         else             return fielddef[col].fieldname;     }

   public char getFieldType(int col)     {         if(col >= numfields)             throw new IllegalArgumentException("Dbf->column number specified is invalid. It's higher than the amount of columns available".concat(String.valueOf(String.valueOf(numfields))));         else             return fielddef[col].fieldtype;     }

   private void init(LEDataInputStream sfile)         throws DbfFileException, IOException     {         DbfFileHeader head = new DbfFileHeader(sfile);         dFile = sfile;         fielddef = new DbfFieldDef[numfields];         int widthsofar = 1;         for(int index = 0; index < numfields; index++)         {             fielddef[index] = new DbfFieldDef();             fielddef[index].setup(widthsofar, sfile);             widthsofar += fielddef[index].fieldlen;         }

       sfile.skipBytes(1);         if(!isFile)             records = GrabFile();     }

   public Record GetNextDbfRec()         throws IOException     {         return records[position++];     }

   private Record GrabNextDbfRec()         throws IOException     {         Record record = new Record(numfields);         int len;                  dFile.skipBytes(1);                  for(int index = 0; index < numfields; index++)         {          len = fielddef[index].fieldlen;          StringBuffer oneRec = new StringBuffer(len);          byte strbuf[] = new byte[len];          for(int i = 0; i < len; i++)                   strbuf = dFile.readByte();                   oneRec.append(new String(strbuf));                   record.addData(oneRec, index);         }                       return record;     }

   private Record[] GrabFile()         throws IOException     {         Record records[] = new Record[last_rec];         for(int i = 0; i < last_rec; i++)             records = GrabNextDbfRec();         return records;     }

   public Record GetDbfRec(int row)         throws IOException     {         Record record;         int len;                  if(!isFile)             return records[row];         record = new Record(numfields);         rFile.seek(data_offset + rec_size * row);                                for(int index = 0; index < numfields; index++)         {          len = fielddef[index].fieldlen;          StringBuffer oneRec = new StringBuffer(len);          byte strbuf[] = new byte[len];          for(int i = 0; i < len; i++)                   strbuf = dFile.readByte();                   oneRec.append(new String(strbuf));                   record.addData(oneRec, index);         }                       return record;     }

   public Vector ParseDbfRecord(int row)         throws IOException     {         return ParseRecord(GetDbfRec(row));     }

   public Vector ParseRecord(Record rec)     {         Vector record = new Vector(numfields);         Integer I = new Integer(0);         Float F = new Float(0.0D);                 for(int i = 0; i < numfields; i++)             switch(fielddef.fieldtype)             {             default:                 break;

           case 67: // 'C'                 record.addElement((rec.getData(i)).toString());                 break;

           case 70: // 'F'             case 78: // 'N'                 if(fielddef.fieldnumdec == 0)                 {                     try                     {                         record.addElement(Integer.decode(PeshStringAproach((rec.getData(i)).toString())));                     }                     catch(NumberFormatException e)                     {                         record.addElement(new Integer(0));                     }                     break;                 }                 try                 {                     record.addElement(Float.valueOf(PeshStringAproach((rec.getData(i)).toString())));                 }                 catch(NumberFormatException e)                 {                     record.addElement(new Float(0.0D));                 }                 break;             }         return record;     }

   String PeshStringAproach(String s)     {         return ReplToSpecSysmb(s, " ", "", 0);     }

   String ReplToSpecSysmb(String s, String delet, String insert, int offs)     {         if(s == null)             return "";         for(int n = 0; (n = s.indexOf(delet, n)) >= 0;)         {             String res = String.valueOf(s.substring(0, n - offs)) + String.valueOf(insert);             int k = res.length();             try             {                 res = String.valueOf(res) + String.valueOf(s.substring(n + 1));             }             catch(StringIndexOutOfBoundsException stringindexoutofboundsexception) { }             n = k;             s = res;         }

       return s;     }

   public Integer[] getIntegerCol(int col)         throws DbfFileException, IOException     {         return getIntegerCol(col, 0, last_rec);     }

   public Integer[] getIntegerCol(int col, int start, int end)         throws DbfFileException, IOException     {         Integer column[] = new Integer[end - start];                 Record sb = new Record(numfields);         String record;         int k = 0;         int i = 0;         if(col >= numfields)             throw new DbfFileException("Dbf->No Such Column in file: ".concat(String.valueOf(String.valueOf(col))));         if(fielddef[col].fieldtype != 'N')             throw new DbfFileException(String.valueOf(String.valueOf((new StringBuffer("Dbf->Column ")).append(col).append(" is not Integer ").append(fielddef[col].fieldtype))));         if(start < 0)             throw new DbfFileException("Dbf->Start must be >= 0");         if(end > last_rec)             throw new DbfFileException("Dbf->End must be <= ".concat(String.valueOf(String.valueOf(last_rec))));         try         {             for(i = start; i < end; i++)             {                                 sb = GetDbfRec(i);                 record = (sb.getData(col)).toString();                 column[i - start] = new Integer(record);             }         }         catch(NumberFormatException nfe)         {             column[i - start] = new Integer(0);         }         catch(EOFException e)         {             System.err.println(e);             System.err.println(String.valueOf(String.valueOf((new StringBuffer("Dbf->record ")).append(i).append(" byte ").append(k).append(" file pos "))));         }         catch(IOException e)         {             System.err.println(e);             System.err.println(String.valueOf(String.valueOf((new StringBuffer("Dbf->record ")).append(i).append(" byte ").append(k).append(" file pos "))));         }         return column;     }

   public Float[] getFloatCol(int col)         throws IOException, DbfFileException     {         return getFloatCol(col, 0, last_rec);     }

   public Float[] getFloatCol(int col, int start, int end)         throws IOException, DbfFileException     {         Float column[] = new Float[end - start];         Record sb = new Record(numfields);         String record;         int k = 0;         int i = 0;         if(col >= numfields)             throw new DbfFileException("Dbf->No Such Column in file: ".concat(String.valueOf(String.valueOf(col))));         if(fielddef[col].fieldtype != 'F' ;; fielddef[col].fieldtype != 'N')             throw new DbfFileException(String.valueOf(String.valueOf((new StringBuffer("Dbf->Column ")).append(col).append(" is not Float ").append(fielddef[col].fieldtype))));         if(start < 0)             throw new DbfFileException("Dbf->Start must be >= 0");         if(end > last_rec)             throw new DbfFileException("Dbf->End must be <= ".concat(String.valueOf(String.valueOf(last_rec))));         try         {             for(i = start; i < end; i++)             {                                 sb = GetDbfRec(i);                 record = (sb.getData(col)).toString();                 String st = record.trim();                 if(st.indexOf(46) == -1)                     st = st.concat(".0");                 try                 {                     column[i - start] = new Float(st);                 }                 catch(NumberFormatException e)                 {                     column[i - start] = new Float(0.0D);                 }             }         }         catch(EOFException e)         {             System.err.println("Dbf->".concat(String.valueOf(String.valueOf(e))));             System.err.println(String.valueOf(String.valueOf((new StringBuffer("Dbf->record ")).append(i).append(" byte ").append(k).append(" file pos "))));         }         catch(IOException e)         {             System.err.println("Dbf->".concat(String.valueOf(String.valueOf(e))));             System.err.println(String.valueOf(String.valueOf((new StringBuffer("Dbf->record ")).append(i).append(" byte ").append(k).append(" file pos "))));         }         return column;     }

   public String[] getStringCol(int col)         throws IOException, DbfFileException     {         return getStringCol(col, 0, last_rec);     }

   public String[] getStringCol(int col, int start, int end)         throws IOException, DbfFileException     {         String column[] = new String[end - start];                 Record sb = new Record(numfields);         String record;         int k = 0;         int i = 0;         if(col >= numfields)             throw new DbfFileException("Dbf->No Such Column in file: ".concat(String.valueOf(String.valueOf(col))));         if(start < 0)             throw new DbfFileException("Dbf->Start must be >= 0");         if(end > last_rec)             throw new DbfFileException("Dbf->End must be <= ".concat(String.valueOf(String.valueOf(last_rec))));         try         {             for(i = start; i < end; i++)             {                 sb = GetDbfRec(i);                 record = (sb.getData(col)).toString();                 column[i - start] = record;             }

       }         catch(EOFException e)         {             System.err.println("Dbf->".concat(String.valueOf(String.valueOf(e))));             System.err.println(String.valueOf(String.valueOf((new StringBuffer("Dbf->record ")).append(i).append(" byte ").append(k).append(" file pos "))));         }         catch(IOException e)         {             System.err.println("Dbf->".concat(String.valueOf(String.valueOf(e))));             System.err.println(String.valueOf(String.valueOf((new StringBuffer("Dbf->record ")).append(i).append(" byte ").append(k).append(" file pos "))));         }         return column;     } }

喜欢0 评分0
默认头像
路人甲
路人甲
  • 注册日期2005-03-22
  • 发帖数46
  • QQ
  • 铜币217枚
  • 威望0点
  • 贡献值0点
  • 银元0个
1楼#
发布于:2005-11-12 15:06
<IMG src="http://www.gisempire.com/bbs/Skins/Default/emot/em02.gif">
举报 回复(0) 喜欢(0)     评分
默认头像
路人甲
路人甲
  • 注册日期2003-10-18
  • 发帖数118
  • QQ
  • 铜币400枚
  • 威望0点
  • 贡献值0点
  • 银元0个
2楼#
发布于:2005-07-07 20:17

现在还在系统建设阶段,没有做界面。界面准备放到最后做。不过现在有一个很简单的测试界面,不知道怎么放上来。

举报 回复(0) 喜欢(0)     评分
默认头像
gis
管理员
管理员
  • 注册日期2003-07-16
  • 发帖数15951
  • QQ
  • 铜币25345枚
  • 威望15368点
  • 贡献值0点
  • 银元0个
  • GIS帝国居民
  • 帝国沙发管家
  • GIS帝国明星
  • GIS帝国铁杆
3楼#
发布于:2005-07-05 10:13

大家有没用geotools做出来的界面什么的,发个上来看看,呵呵

偶还没用过,不好意思发言

GIS麦田守望者,期待与您交流。
举报 回复(0) 喜欢(0)     评分
默认头像
金牌卧底
金牌卧底
  • 注册日期2004-09-08
  • 发帖数316
  • QQ
  • 铜币45枚
  • 威望0点
  • 贡献值0点
  • 银元0个
  • GIS帝国居民
4楼#
发布于:2005-05-30 12:20

我们来世不易,要不开心的生活,岂非有负此一生!我命由我不由天,事在人为,做一个生活强者! I LOVE GIS(INCLUE 3S SYSTEM) ,AND YOU?
举报 回复(0) 喜欢(0)     评分
默认头像

返回顶部