用JavaDBF操作(讀、寫)DBF文件

JavaDBF操作(讀、寫)DBF文件<o:p></o:p>

最近的一個項目需要動態生成DBF文件,用到JavaDBF,簡單介紹一下<o:p></o:p>

官方網站:http://javadbf.sarovar.org/<o:p></o:p>

官方英文指南:http://sarovar.org/docman/view.php/32/23/javadbf-tutorial.html<o:p></o:p>

最新版本:<st1:chsdate w:st="on" isrocdate="False" islunardate="False" day="30" month="12" year="1899">0.4.0</st1:chsdate>,最後發佈時間還是在<st1:chsdate w:st="on" isrocdate="False" islunardate="False" day="1" month="4" year="2004">200441</st1:chsdate>,看來DBF真是老了。老歸老,有些時候還是得用。<o:p></o:p>

下面是分別是讀取和寫DBF文件以及其他操作函數(關鍵信息的解釋我放在了註釋裏,這樣看起來會更方便):<o:p></o:p>

讀取DBF文件:<o:p></o:p>

<o:p>
java 代碼
  1. public static void readDBF(String path)   
  2.   
  3.     {   
  4.   
  5.        InputStream fis = null;   
  6.   
  7.         try    
  8.   
  9.         {   
  10.   
  11.             //讀取文件的輸入流   
  12.   
  13.             fis  = new FileInputStream(path);   
  14.   
  15.             //根據輸入流初始化一個DBFReader實例,用來讀取DBF文件信息   
  16.   
  17.             DBFReader reader = new DBFReader(fis);    
  18.   
  19.             //調用DBFReader對實例方法得到path文件中字段的個數   
  20.   
  21.             int fieldsCount = reader.getFieldCount();   
  22.   
  23.             //取出字段信息   
  24.   
  25.             forint i=0; i<fieldsCount; i++)    
  26.   
  27.             {   
  28.   
  29.               DBFField field = reader.getField(i);   
  30.   
  31.               System.out.println(field.getName());   
  32.   
  33.             }   
  34.   
  35.             Object[] rowValues;   
  36.   
  37.             //一條條取出path文件中記錄   
  38.   
  39.             while((rowValues = reader.nextRecord()) != null)    
  40.   
  41.             {   
  42.   
  43.               forint i=0; i<rowValues.length; i++)    
  44.   
  45.               {   
  46.   
  47.                 System.out.println(rowValues[i]);   
  48.   
  49.               }   
  50.   
  51.             }   
  52.   
  53.           }   
  54.   
  55.           catch(Exception e)    
  56.   
  57.           {   
  58.   
  59.           e.printStackTrace();   
  60.   
  61.           }   
  62.   
  63.           finally  
  64.   
  65.           {   
  66.   
  67.           try{   
  68.   
  69.                fis.close();   
  70.   
  71.           }catch(Exception e){}   
  72.   
  73.           }   
  74.   
  75.     }   

 

DBF文件:

java 代碼

  1. public static void writeDBF(String path)   
  2.   
  3.   
  4.   
  5.  OutputStream fos = null;   
  6.   
  7.  try  
  8.   
  9.  {   
  10.   
  11.      //定義DBF文件字段   
  12.   
  13.      DBFField[] fields = new DBFField[3];   
  14.   
  15.      //分別定義各個字段信息,setFieldName和setName作用相同,   
  16.   
  17.      //只是setFieldName已經不建議使用   
  18.   
  19.      fields[0] = new DBFField();   
  20.   
  21.      //fields[0].setFieldName("emp_code");   
  22.   
  23.      fields[0].setName("semp_code");   
  24.   
  25.      fields[0].setDataType(DBFField.FIELD_TYPE_C);   
  26.   
  27.      fields[0].setFieldLength(10);   
  28.   
  29.   
  30.   
  31.      fields[1] = new DBFField();   
  32.   
  33.      //fields[1].setFieldName("emp_name");   
  34.   
  35.      fields[1].setName("emp_name");   
  36.   
  37.      fields[1].setDataType(DBFField.FIELD_TYPE_C);   
  38.   
  39.      fields[1].setFieldLength(20);   
  40.   
  41.   
  42.   
  43.      fields[2] = new DBFField();   
  44.   
  45.      //fields[2].setFieldName("salary");   
  46.   
  47.      fields[2].setName("salary");   
  48.   
  49.      fields[2].setDataType(DBFField.FIELD_TYPE_N);   
  50.   
  51.      fields[2].setFieldLength(12);   
  52.   
  53.      fields[2].setDecimalCount(2);   
  54.   
  55.   
  56.   
  57.      //DBFWriter writer = new DBFWriter(new File(path));   
  58.   
  59.      //定義DBFWriter實例用來寫DBF文件   
  60.   
  61.      DBFWriter writer = new DBFWriter();   
  62.   
  63.      //把字段信息寫入DBFWriter實例,即定義表結構   
  64.   
  65.      writer.setFields(fields);   
  66.   
  67.      //一條條的寫入記錄   
  68.   
  69.      Object[] rowData = new Object[3];   
  70.   
  71.      rowData[0] = "1000";   
  72.   
  73.      rowData[1] = "John";   
  74.   
  75.      rowData[2] = new Double(5000.00);   
  76.   
  77.      writer.addRecord(rowData);   
  78.   
  79.      rowData = new Object[3];   
  80.   
  81.      rowData[0] = "1001";   
  82.   
  83.      rowData[1] = "Lalit";   
  84.   
  85.      rowData[2] = new Double(3400.00);   
  86.   
  87.      writer.addRecord(rowData);   
  88.   
  89.      rowData = new Object[3];   
  90.   
  91.      rowData[0] = "1002";   
  92.   
  93.      rowData[1] = "Rohit";   
  94.   
  95.      rowData[2] = new Double(7350.00);   
  96.   
  97.      writer.addRecord(rowData);   
  98.   
  99.      //定義輸出流,並關聯的一個文件   
  100.   
  101.      fos = new FileOutputStream(path);   
  102.   
  103.      //寫入數據   
  104.   
  105.      writer.write(fos);   
  106.   
  107.      //writer.write();   
  108.   
  109.  }catch(Exception e)   
  110.   
  111.  {   
  112.   
  113.      e.printStackTrace();   
  114.   
  115.  }   
  116.   
  117.  finally  
  118.   
  119.  {   
  120.   
  121.      try{   
  122.   
  123.      fos.close();   
  124.   
  125.      }catch(Exception e){}   
  126.   
  127.  }   

注意:writer.addRecord(rowData)時並不真正寫入數據,在最後writer.write(fos)時纔會把數據寫入DBF文件,之前addRecord的數據暫時存放在內存中。如果數據量過大,這種方式顯然不適合,內存中存儲的數據過多,所以JavaDBF提供了另外一種機制來解決這個問題:Sync Mode(同步模式)。使用方法如下:

new DBFWriter(new File(path))實例化DBFWriter類,最後寫入數據時用writer.write(),這樣在每次addRecord時數據就被寫入的DBF文件中。

因爲初始化DBFWriter時傳遞了DBF文件,所以不用再定義DBF表結構,如果你定義並加載表結構會報異常。

 

 下面這個函數會根據你傳入的數據信息自動生成DBF文件,這樣以後我們只要構造好數組,就可以直接得到DBF文件,不用每次都去寫重複的代碼。

 

java 代碼
  1. public static void generateDbfFromArray(   
  2.   
  3.                                    String dbfName,   
  4.   
  5.                                    String[] strutName,   
  6.   
  7.                                    byte[] strutType,   
  8.   
  9.                                    int[] strutLength,   
  10.   
  11.                                    Object[][] data   
  12.   
  13.                                   )   
  14.   
  15. {   
  16.   
  17.    OutputStream fos = null;   
  18.   
  19.    try  
  20.   
  21.    {   
  22.   
  23.        int fieldCount = strutName.length;   
  24.   
  25.        DBFField[] fields = new DBFField[fieldCount];   
  26.   
  27.        for(int i=0;i<fieldCount;i++)   
  28.   
  29.        {   
  30.   
  31.           fields[i] = new DBFField();   
  32.   
  33.           fields[i].setName(strutName[i]);   
  34.   
  35.           fields[i].setDataType(strutType[i]);   
  36.   
  37.           fields[i].setFieldLength(strutLength[i]);   
  38.   
  39.        }   
  40.   
  41.        DBFWriter writer = new DBFWriter();   
  42.   
  43.        writer.setFields(fields);   
  44.   
  45.        for(int i=0;i<fieldCount;i++)   
  46.   
  47.        {   
  48.   
  49.        writer.addRecord(data[i]);   
  50.   
  51.        }   
  52.   
  53.        fos = new FileOutputStream(dbfName);   
  54.   
  55.        writer.write(fos);   
  56.   
  57.    }   
  58.   
  59.    catch(Exception e)   
  60.   
  61.    {   
  62.   
  63.        e.printStackTrace();   
  64.   
  65.    }   
  66.   
  67.    finally  
  68.   
  69.    {   
  70.   
  71.        try{   
  72.   
  73.        fos.close();   
  74.   
  75.        }catch(Exception e){}   
  76.   
  77.    }   
  78.   
  79. }  

 

可以看到定義JavaDBF表結構或者添加數據時是通過傳遞數組實現,也就是說只要我們有了這些用來構造表結果和表示結果集的數組就有了DBF文件,那麼我們可以通過類似下面這樣的函數把ResultSet信息轉換成數組信息。

java 代碼
  1. public static void ResultsetToArray(ResultSet rs)   
  2.   
  3. {   
  4.   
  5.    try  
  6.   
  7.    {   
  8.   
  9.        ResultSetMetaData meta = rs.getMetaData();   
  10.   
  11.        int columnCount = meta.getColumnCount();   
  12.   
  13.        String[] strutName = new String[columnCount];   
  14.   
  15.        byte[] strutType = new byte[columnCount];   
  16.   
  17.        rs.last();   
  18.   
  19.        int itemCount = rs.getRow();   
  20.   
  21.        rs.first();   
  22.   
  23.        Object[][] data = new Object[columnCount][itemCount];   
  24.   
  25.        for(int i=0;i<columnCount;i++)   
  26.   
  27.        {   
  28.   
  29.           strutType[i] = (byte)meta.getColumnType(i);   
  30.   
  31.           strutName[i] = meta.getColumnName(i);   
  32.   
  33.        }   
  34.   
  35.   
  36.   
  37.        for(int i=0;rs.next();i++)   
  38.   
  39.        {      
  40.   
  41.           for(int j=0;j<columnCount;j++)   
  42.   
  43.           {   
  44.   
  45.               data[i][j] = rs.getObject(j);   
  46.   
  47.           }   
  48.   
  49.        }   
  50.   
  51.    }   
  52.   
  53.    catch(Exception e)   
  54.   
  55.    {   
  56.   
  57.        e.printStackTrace();   
  58.   
  59.    }   
  60.   
  61. }  

細心的讀者可能會發現:strutType[i] = (byte)meta.getColumnType(i)這條語句是不可靠的,的卻,這裏的代碼我省略了,JavaDBF中的字段類型表示和ResultSetMetaData中的字段類型表示應該是不一致的,這裏做一個類型映射和轉換即可。

 

 

 

</o:p>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章