代码之家  ›  专栏  ›  技术社区  ›  matiasf

分析多行固定宽度文件

  •  0
  • matiasf  · 技术社区  · 14 年前

    我有一个固定宽度的平锉。更糟的是,每行可以是一个新记录,也可以是上面一行的子记录,由每行的第一个字符标识:

    A0020SOME DESCRIPTION   MORE DESCRIPTION 922 2321      # Separate
    A0021ANOTHER DESCRIPTIONMORE DESCRIPTION 23111442      # records
    B0021ANOTHER DESCRIPTION   THIS TIME IN ANOTHER FORMAT # sub-record of record "0021"
    

    我试过用 Flatworm

    "Repeating segments are supported only for delimited files"
    

    3 回复  |  直到 9 年前
        1
  •  2
  •   Federico Fissore    12 年前

    你看过jrecordbind吗?

    http://jrecordbind.org/

    “jrecordbind支持分层固定长度文件:某些类型的记录是其他记录类型的子记录。”

        2
  •  0
  •   Wilfred Springer    14 年前

    检查 Preon . 虽然preon的目标是位流压缩数据,但您可能能够扭转它的手臂,并将其用于标识的文件格式。使用preon的好处是它也可以生成人类可读的文档。

        3
  •  0
  •   josliber Martin Ballet    9 年前

    uniVocity-parsers

    //1st, use a RowProcessor for the "detail" rows.
    ObjectRowListProcessor detailProcessor = new ObjectRowListProcessor();
    
    //2nd, create MasterDetailProcessor to identify whether or not a row is the master row.
    // the row placement argument indicates whether the master detail row occurs before or after a sequence of "detail" rows.
    MasterDetailListProcessor masterRowProcessor = new MasterDetailListProcessor(RowPlacement.TOP, detailProcessor) {
        @Override
        protected boolean isMasterRecord(String[] row, ParsingContext context) {
            //Returns true if the parsed row is the master row.
            return row[0].startsWith("B");
        }
    };
    
    FixedWidthParserSettings parserSettings = new FixedWidthParserSettings(new FixedWidthFieldLengths(4, 5, 40, 40, 8));
    
    // Set the RowProcessor to the masterRowProcessor.
    parserSettings.setRowProcessor(masterRowProcessor);
    
    FixedWidthParser parser = new FixedWidthParser(parserSettings);
    parser.parse(new FileReader(yourFile));
    
    // Here we get the MasterDetailRecord elements.
    List<MasterDetailRecord> rows = masterRowProcessor.getRecords();
    for(MasterDetailRecord masterRecord = rows){
     // The master record has one master row and multiple detail rows.
        Object[] masterRow = masterRecord.getMasterRow();
        List<Object[]> detailRows = masterRecord.getDetailRows();
    }