2.3.7. fejezet, DBF fájl feldolgozás
Beküldte pzoli - 2011, május 16 - 4:05du
Tömör adatszerkezete miatt a DBase fájlokat manapság is alkalmazzák adattovábbításra. Ezeket a fájlokat a JavaDBF csomaggal olvashatjuk és írhatjuk.
package com.omega.engine.containers; import com.omega.configuration.ContainerDescriptor; import com.omega.configuration.ResourceContainer; import com.omega.engine.ContainerGroup; import com.omega.engine.Field; import com.omega.engine.RecordDescriptor; import com.omega.engine.Request; import com.omega.engine.UnifiedRecordType; import java.io.*; import java.util.HashMap; import java.util.TreeMap; import com.linuxense.javadbf.DBFField; import com.linuxense.javadbf.*; public class CachedReadOnlyDBFContainer extends AbstractMemoryCacheContainer { private InputStream inputStream; private DBFReader reader; private int DBFRecordPointer; private HashMap<Byte, String> fTypes; public CachedReadOnlyDBFContainer(ContainerDescriptor containerDescriptor, ContainerGroup containerGroup) { super(containerDescriptor, containerGroup); fTypes = new HashMap<Byte, String>(); fTypes.put(DBFField.FIELD_TYPE_C, "java.lang.String"); fTypes.put(DBFField.FIELD_TYPE_L, "java.lang.Boolean"); fTypes.put(DBFField.FIELD_TYPE_N, "java.lang.Double"); fTypes.put(DBFField.FIELD_TYPE_F, "lava.lang.Double"); fTypes.put(DBFField.FIELD_TYPE_D, "java.util.Date"); fTypes.put(DBFField.FIELD_TYPE_M, ""); } @Override public boolean regenerateRecordDescriptor() { boolean result = reader != null; if (result) { TreeMap<Integer, Field> fieldList = new TreeMap<Integer, Field>(); TreeMap<String, Field> fieldIndex = new TreeMap<String, Field>(); try { for (int i = 1; i <= reader.getFieldCount(); i++) { DBFField dbfield = reader.getField(i); String name = dbfield.getName(); String type = fTypes.get(dbfield.getDataType()); TreeMap<String, String> titles = new TreeMap<String, String>(); Field field = new Field(name, i, type, null, true, false, false, null, null, titles); fieldList.put(i, field); fieldIndex.put(name, field); } } catch (DBFException ex) { result = false; writeLog(ex); } catch (Exception ex) { result = false; writeLog(ex); } setRecordDescriptor(new RecordDescriptor(fieldList, fieldIndex)); } return result; } @Override public boolean setRecordPointer(int value) { boolean result = (value > 0) && (value <= getSize()); if (result) { try { Object[] row; while ((DBFRecordPointer < value) && ((row = reader.nextRecord()) != null)) { UnifiedRecordType record = new UnifiedRecordType(row, getRecordDescriptor()); appendRecord(record, false); DBFRecordPointer++; } super.setRecordPointer(value); } catch (DBFException ex) { result = false; writeLog(ex); } } return result; } @Override public void close() { reader = null; if (inputStream != null) try { inputStream.close(); } catch (IOException ex) { writeLog(ex); } } @Override public boolean open(Request request) { boolean result = true; try { String fn = request.getParamAsDecodedString("dbfile", false); String charSet = request.getParamAsDecodedString("charset", false); inputStream = new FileInputStream(fn); reader = new DBFReader(inputStream); if (charSet != null) reader.setCharactersetName(charSet); DBFRecordPointer = 0; } catch (DBFException ex) { result = false; ResourceContainer resourceContainer = getResourceContainer(); try { inputStream.close(); inputStream = null; } catch (IOException e) { resourceContainer.writeLog(e); } resourceContainer.writeLog(ex); } catch (IOException ex) { result = false; writeLog(ex); } return result; } @Override public int getSize() { return (reader == null ? 0 : reader.getRecordCount()); } }
A DBFReader egy irányba olvasó osztály, ezért egy memória konténerbe ágyazva segíthetjük az adatok gyors kétirányú elérését.
- A hozzászóláshoz be kell jelentkezni