📄 dbffile.java
字号:
public static DbfTableModel getDbfTableModel(URL dbf) { return getDbfTableModel(dbf.toString()); } /** * Creates a DbfTableModel for a given .dbf file * * @param dbf The path of the file to retrieve. * @return The DbfTableModel, null if there is a problem. */ public static DbfTableModel getDbfTableModel(String dbf) { DbfFile model = null; try { BinaryBufferedFile bbf = new BinaryBufferedFile(dbf); model = new DbfFile(bbf); } catch (Exception exception) { if (Debug.debugging("shape")) { Debug.error("problem loading DBF file" + exception.getMessage()); } } return model; } public static void main(String[] args) { Debug.init(); ArgParser ap = new ArgParser("DbfFile"); ap.add("columns", "Print field header information."); ap.add("mask", "Only show listed columns", -1); ap.add("source", "The dbf file to read.", 1); ap.add("target", "The dbf file to write, use with mask to remove columns into new dbf file.", 1); ap.add("num", "Specify the number of records to read and display (handy for large dbf files)", 1); if (!ap.parse(args)) { ap.printUsage(); System.exit(0); } String source = null; String target = null; double num = Double.MAX_VALUE; String[] ags = ap.getArgValues("source"); if (ags != null) { source = ags[0]; } else { source = FileUtils.getFilePathToOpenFromUser("Choose DBF file"); if (source == null) { System.exit(0); } } ags = ap.getArgValues("target"); if (ags != null) { target = ags[0]; } boolean readData = ap.getArgValues("columns") == null; ags = ap.getArgValues("num"); if (ags != null) { try { num = Double.parseDouble(ags[0]); } catch (NumberFormatException nfe) { } } String[] columnMask = ap.getArgValues("mask"); String[] columns = ap.getArgValues("columns"); try { DbfFile dtm = (DbfFile) DbfFile.getDbfTableModel(source); if (columns != null) { dtm.setColumnMask(columnMask); } if (target != null) { OutputStream os = new FileOutputStream(target); DbfOutputStream dos = new DbfOutputStream(os); dos.writeModel(dtm); } else { if (readData) { dtm.readData(0, (int) num); } dtm.setWritable(true); dtm.exitOnClose = true; dtm.showGUI(args[0], MODIFY_ROW_MASK | MODIFY_COLUMN_MASK | SAVE_MASK); } } catch (Exception e) { Debug.error(e.getMessage()); e.printStackTrace(); } } /** * Checks the _columnMask Object[] and looks for Boolean.TRUE objects, * indicating a column that should be used. Returns a boolean[] with trues * in the indexes for those columns. * * @return */ public boolean[] getColumnMask() { boolean[] columnMask = new boolean[_columnMask.length]; for (int i = 0; i < _columnMask.length; i++) { columnMask[i] = _columnMask[i] == Boolean.TRUE; } return columnMask; } /** * Given a boolean[] where trues mark columns to keep, a _columnMask * Object[] is set on this object with Boolean.TRUE objects in that array * for the trues, and Integer objects representing the lengths of the false * columns. The lengths are used when reading the dbf file, so it's known * how many bytes to skip for that column. * * @param mask */ protected void createColumnMaskArray(boolean[] mask) { if (mask != null && mask.length <= _columnCount) { _columnMask = new Object[mask.length]; for (int i = 0; i < mask.length; i++) { if (mask[i] == true) { _columnMask[i] = Boolean.TRUE; } else { _columnMask[i] = new Integer(_lengths[i]); } } resolveColumns(); } } /** * Limit which columns are read from the dbf file using a boolean array * corresponding to the columns. For indexes in the array marked true, those * columns will be read. If the column mask has already been set, the dbf * file header will be re-read to reset the metadata for the file. * * @param mask */ public void setColumnMask(boolean[] mask) { try { if (_columnMask != null) { readHeader(bf); } } catch (Exception e) { Debug.error("problem setting column mask for DbfFile" + e.getMessage()); } createColumnMaskArray(mask); } /** * Limit which columns are read from the dbf file using the column names. If * the column mask has already been set, the dbf file header will be re-read * to reset the metadata for the file. * * @param columnNames */ public void setColumnMask(String[] columnNames) { try { if (_columnMask != null) { readHeader(bf); } } catch (Exception e) { Debug.error("problem setting column mask for DbfFile" + e.getMessage()); } if (columnNames != null && _names != null) { boolean[] mask = new boolean[_names.length]; for (int j = 0; j < _names.length; j++) { for (int i = 0; i < columnNames.length; i++) { if (_names[j].equalsIgnoreCase(columnNames[i])) { mask[j] = true; break; } } } createColumnMaskArray(mask); } } /** * Sets the metadata for the dbf file to match the current _columnMask * settings. */ protected void resolveColumns() { if (_columnMask != null && _columnMask.length == _columnCount) { int newColumnCount = 0; for (int i = 0; i < _columnMask.length; i++) { if (_columnMask[i] == Boolean.TRUE) { newColumnCount++; } } ArrayList records = null; if (_records != null) { records = new ArrayList(_rowCount); } int[] lengths = new int[newColumnCount]; byte[] decimalCounts = new byte[newColumnCount]; byte[] types = new byte[newColumnCount]; String[] names = new String[newColumnCount]; int newIndex = 0; for (int i = 0; i < _columnMask.length; i++) { if (_columnMask[i] == Boolean.TRUE) { lengths[newIndex] = _lengths[i]; decimalCounts[newIndex] = _decimalCounts[i]; types[newIndex] = _types[i]; names[newIndex] = _names[i]; if (records != null) { records.add(_records.get(i)); } newIndex++; } } _lengths = lengths; _decimalCounts = decimalCounts; _types = types; _names = names; _columnCount = newColumnCount; if (records != null) { _records = records; } } } public int getHeaderLength() { return _headerLength; } public void setHeaderLength(int length) { _headerLength = length; } public int getRecordLength() { return _recordLength; } public void setRecordLength(int length) { _recordLength = length; } public int getRowCount() { return _rowCount; } public void setRowCount(int count) { _rowCount = count; } public BinaryFile getBinaryFile() { return bf; } public void setBinaryFile(BinaryFile bf) throws EOFException, FormatException, IOException { this.bf = bf; readHeader(bf); } public java.text.DecimalFormat getDecimalFormat() { return df; } public void setDecimalFormat(java.text.DecimalFormat df) { this.df = df; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -