You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Flatworm读取平面文件并将指定号码加载到List<String>属性?

用Flatworm提取电话号码到Attribute列表的解决方案

要实现把输入记录里的最后3个电话号码加载到你的attribute列表里,我们可以通过Flatworm的字段映射+实体类setter方法来完成,具体步骤如下:

1. 完善你的实体类

首先在现有实体类基础上,添加三个电话号码的setter方法,每个setter内部调用你已经写好的addAttribute()方法,自动把号码存入列表:

private List<String> attribute = new ArrayList<String>();
public List<String> getAttribute() { return attribute; }
public void setAttribute(List<String> attribute) { this.attribute = attribute; }
public void addAttribute(String atr) { attribute.add(atr); }

// 新增三个电话号码的setter,自动将号码加入列表
public void setPhone1(String phone) {
    addAttribute(phone.trim());
}

public void setPhone2(String phone) {
    addAttribute(phone.trim());
}

public void setPhone3(String phone) {
    addAttribute(phone.trim());
}

2. 编写Flatworm配置文件

创建一个XML配置文件(比如contact-record-config.xml),定义输入记录的固定宽度结构(因为你的名字字段包含空格,用分隔符会拆分错误,固定宽度更可靠):

<flatworm-config>
  <record-definitions>
    <!-- 总长度:名字15位 + 3个电话号码各10位 = 45位 -->
    <record name="contactRecord" recordLength="45">
      <!-- 名字字段:从第1位开始,占15个字符,自动去除前后空格 -->
      <field name="fullName" type="string" startPosition="1" length="15" trim="true" />
      <!-- 三个电话号码字段,分别映射到实体类的setter方法 -->
      <field name="phone1" type="string" startPosition="16" length="10" trim="true" setter="setPhone1" />
      <field name="phone2" type="string" startPosition="26" length="10" trim="true" setter="setPhone2" />
      <field name="phone3" type="string" startPosition="36" length="10" trim="true" setter="setPhone3" />
    </record>
  </record-definitions>
</flatworm-config>

3. 编写解析代码

用Flatworm的FileParser加载配置,解析输入记录并绑定到你的实体类:

import com.phillips.flatworm.FileParser;
import com.phillips.flatworm.config.ConfigurationReader;

public class ContactParser {
    public static void main(String[] args) throws Exception {
        // 加载Flatworm配置文件
        ConfigurationReader configReader = new ConfigurationReader();
        FileParser parser = configReader.configureParser("contact-record-config.xml");
        
        // 初始化你的实体类实例
        YourEntity entity = new YourEntity();
        
        // 输入记录
        String inputRecord = "NHJames Elliot 4183306636 4189965965 4182235656";
        
        // 解析记录并自动调用setter,将号码加入attribute列表
        parser.parseRecord(inputRecord, entity);
        
        // 验证结果:输出三个电话号码
        System.out.println("Attribute列表内容:" + entity.getAttribute());
    }
}

关键说明

  • 如果你的输入记录长度或字段宽度有变化,只需要调整配置文件里的recordLength、startPosition和length参数即可。
  • 用trim="true"可以自动去除号码前后可能存在的空格,保证列表里的号码格式干净。
  • 如果你的输入是用其他分隔符(而非固定宽度),且名字字段不含空格,也可以把配置改成分隔符模式,替换<record>标签为<record name="contactRecord" delimiter=" ">,并去掉recordLength、startPosition、length参数。

内容的提问来源于stack exchange,提问作者jj sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:07:38