如何将分隔的平面文件解析为POJO

时间:2012-12-13 05:09:14

标签: java

感谢有人可以指出并建议如何将扁平管道分隔文件解析为JAVA Pojo。

例如。 平面文件 0001 | XYZ | 120

需要将其读入具有

的POJO
public class pojo {

private String acct;
private String customer;
private int balance;
}

我可以将整个输入文件作为集合读取,但是,最终会将每个标记设置为pojo成员。相反,我想解析pojo成员。类似于CASTOR XML映射到POJO的东西。

在这方面感谢任何帮助。提前致谢

5 个答案:

答案 0 :(得分:7)

您可以使用Bean IO。我一直在广泛使用它。

将XML配置为此类

<stream name="employees" format="delimited" strict="true">
  <parser>  
    <property name="delimiter" value="|" />
  </parser>
  <record name="test" class="example.Pojo" minOccurs="1" maxOccurs="1">
      <field name="act" />
      <field name="customer" />
      <field name="balance" type="int" />
    </record>
 </stream>

有关详细信息,请参阅here

答案 1 :(得分:3)

OopenCSV http://opencsv.sourceforge.net/有你想要的东西。只需将分隔符从|更改为,即可。你应该全力以赴。

答案 2 :(得分:1)

我只想一次读取一行,拆分值并调用POJO构造函数(如果不可用,创建一个),例如:

   List<pojo> pojoList = new ArrayList<pojo>();
   BufferedReader br = new BufferedReader(new FileReader("FlatFile.txt"));
   String line = "";
   while((line = br.readLine()) != null) {  
       String[] fields = line.split("\|");
       pojo p = new pojo(fields[0], fields[1], fields[2]);
       pojoList.add(p);
   }

答案 3 :(得分:0)

感谢大家的快速回复。根据Thihara的建议,设法让OPENCSV工作(感谢Glen Smith和Kyle Miller对Bean Mapping的贡献)从OPENCSV获取opencsv-2.3.jar

我正在发布完整的资源,以使像我这样的人受益。

输入文件

/**
     * Input File: acct_os.txt
     * 
     * <pre>
     * 12345|ABC Company|120.45
     * 34567|XYZ Company|45.00
     * 99999|MNC Bank|67.00
     */

/**
 * Bind File to a POJO
 * 
 * @param inputFile
 * @param delim
 * @throws FileNotFoundException
 */
public void bindFileToPojo(String inputFile, char delim) throws FileNotFoundException {

    System.out.println("\n===== Reading to a POJO\n");

    ColumnPositionMappingStrategy<TestCustomerBean> strat = new ColumnPositionMappingStrategy<TestCustomerBean>();
    strat.setType(TestCustomerBean.class);
    /**
     * the fields to bind do in your JavaBean
     */
    String[] columns = new String[] { "acct", "customer", "balance" };
    strat.setColumnMapping(columns);

    CsvToBean<TestCustomerBean> csv = new CsvToBean<TestCustomerBean>();
    /**
     * Read file contents to list using CSVReader
     */
    List<TestCustomerBean> list = csv.parse(strat, new CSVReader(new FileReader(inputFile), delim));
    /**
     * Display column mapping
     */
    displayColumnMapping(strat.getColumnMapping());

    for (TestCustomerBean bean : list) {
        System.out.println("account: ["
                + bean.getAcct()
                    + "] customer: ["
                    + bean.getCustomer()
                    + "] balance: ["
                    + bean.getBalance()
                    + "]");
    }
}

/**
 * Display column mapping
 * 
 * @param columns
 */
private void displayColumnMapping(String[] columns) {
    for (String column : columns) {
        System.out.println("Column Mapping-->" + column);
    }
}

TestCustomerBean(省略了getter / setter)

private String acct;
private String customer;
private Double balance;

输出

=====阅读POJO

列映射 - &gt; acct
列映射 - &gt;客户
列映射 - &gt;余额
帐号:[12345]客户:[ABC公司]余额:[120.45]
帐户:[34567]客户:[XYZ公司]余额:[45.0]
帐号:[99999]客户:[MNC Bank]余额:[67.0]

答案 4 :(得分:0)

根据JayaMohan(谢谢)的建议,另一个论坛建议平滑,我可以使用beanio将Flat文件映射到POJO。您可以获得beanio

使用beanio的完整来源

    /**
 * Read inputFile and map to BeanIO Mapping file and bind to pojo
 * 
 * @param inputFile
 * @param mappingFile
 */
public void flatToBeanReader(String inputFile, String mappingFile) {
    /**
     * create a StreamFactory
     */
    StreamFactory factory = StreamFactory.newInstance();
    /**
     * load the mapping file
     */
    factory.load(mappingFile);
    /**
     * use a StreamFactory to create a BeanReader
     */
    BeanReader in = factory.createReader("customers", new File(inputFile));
    TestCustomerBean cust;
    while ((cust = (TestCustomerBean) in.read()) != null) {
        System.out.println("acct: ["
                + cust.getAcct()
                    + "] customer: ["
                    + cust.getCustomer()
                    + "] balance: ["
                    + cust.getBalance()
                    + "]");
    }
    in.close();
}

映射文件

<?xml version="1.0" encoding="UTF-8"?>

http://www.beanio.org/2012/03/mapping.xsd“&GT;

<stream name="customers" format="delimited" strict="false">
    <parser>
        <property name="delimiter" value="|" />
    </parser>
    <record name="cust" class="TestCustomerBean">

        <field name="acct" />
        <field name="customer" />
        <field name="balance" type="Double" />
    </record>
</stream>

输出将是
帐户:[12345]客户:[ABC公司]余额:[120.45]
acct:[34567]客户:[XYZ公司]余额:[45.0]
acct:[99999]客户:[MNC Bank]余额:[67.0]

相关问题