0

I have a text file with multiple lines(rows) that I am trying to read using BeanIO. The file looks like this:

Adele|Lionel Richie|Hello|22865

Ed Sheeran|Simple Plan|One|11230

Here, the line represents multiple artists that have a song with the same name. Adele and Lionel Richie have a song called "Hello" and the song id is 22865. The number of columns is always 4 in each row

I need to parse this row to create 2 objects of Song

Class Song{
 String artistName;
 String songName;
 int songId;
}

First row above must give 2 Song objects :

Adele , Hello and 22865

Lionel Richie, Hello and 22865

Would this be possible using BeanIO? I have used the delimiter parsing ability of BeanIO to parse but not sure how to get multiple records for a single line

SRm
  • 49
  • 9
  • 1
    Since your records don't have a fixed format, i.e. a fixed number of columns, you can't use automatic object mapping libraries. You need to parse the lines yourself, which should be easy using `BufferedReader` and `split()`, assuming no value can contain a `|` character. – Andreas May 13 '19 at 23:55
  • I looked into the data further, Looks like there are fixed 4 columns on all rows. I have edited the question to mention this – SRm May 14 '19 at 16:34

1 Answers1

0

Follow the guidelines on the documentation:

http://beanio.org/2.0/docs/reference/index.html#RecordGroups

Class Song{
 List<String> artistName;
 String songName;
 int songId;
}

This way you can have as many artists as you want

<record name="artistName" collection="list" >
...
</record>
  • Thank you for the answer and the link! I need two different Song objects from each line in the file. Since eventually they will translate to two rows in a DB table. – SRm Jun 20 '19 at 17:50