4

感谢有人能指出并推荐如何将扁平管道分隔的文件解析为 JAVA Pojo。

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

这需要被读入具有

public class pojo {

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

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

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

4

5 回答 5

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>

有关详细信息,请参阅此处

于 2012-12-13T05:24:32.857 回答
3

OopenCSV http://opencsv.sourceforge.net/有你要找的东西。只需将分隔符更改为|from ,。你应该准备好了。

于 2012-12-13T05:16:35.010 回答
2

我只需一次读取一行,拆分值并调用 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);
   }
于 2012-12-13T05:44:16.377 回答
0

另一个论坛根据 JayaMohan 的建议推荐了 smooooth(谢谢),我可以使用 beanio 将平面文件映射到 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">

<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] 账户
:[34567] 客户:[XYZ 公司] 余额:[45.0]
账户:[99999] 客户:[MNC 银行] 余额:[67.0 ]

于 2012-12-13T09:13:18.177 回答
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

列映射-->acct
列映射-->customer
列映射-->balance
account: [12345] customer: [ABC Company] balance: [120.45]
account: [34567] customer: [XYZ Company] balance: [45.0]
帐户:[99999] 客户:[MNC 银行] 余额:[67.0]

于 2012-12-13T07:43:56.677 回答