0% found this document useful (0 votes)
3 views

8-BeanIO Java tutorial

The BeanIO Java tutorial provides an overview of the BeanIO framework, which is used for marshaling Java beans from various formats like CSV and XML. It includes step-by-step instructions on setting up a project, defining dependencies, creating a main class, and mapping data to a POJO. The tutorial also covers the implementation of a custom type handler for parsing enums and demonstrates the output of the program.

Uploaded by

gs23133
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

8-BeanIO Java tutorial

The BeanIO Java tutorial provides an overview of the BeanIO framework, which is used for marshaling Java beans from various formats like CSV and XML. It includes step-by-step instructions on setting up a project, defining dependencies, creating a main class, and mapping data to a POJO. The tutorial also covers the implementation of a custom type handler for parsing enums and demonstrates the output of the program.

Uploaded by

gs23133
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

06/12/2024, 19:21 BeanIO Java tutorial

800+ Q&As Menu Contact & reviews Register Login

800+ Big Data & Java Interview FAQs


It pays to prepare & choose from 2-6 offers. Read & Write more code to fast-track & go places with confidence.

Select A to Z of Java & Big Data Techs


search here … Go

Home Why?  Career  Membership 

Home › Java & Big Data › Tutorials › Tutorials - Enterprise Java › TUT - Core Java › TUT - CSV and Java ›

BeanIO Java tutorial

BeanIO Java tutorial 300+ Java Interview


Q&As - Quick Prep FAQs

 Posted on January 15, 2015 300+ Java Interview FAQs 📌 


150+ Spring & Hibernate
FAQs 
BeanIO is an open source Java framework for marshaling and 150+ Java Architect FAQs 📌 
marshaling Java beans from a flat file, stream, or simple String
100+ Java code quality Q&As
object. It is very powerful with support for XML, CSV, delimited and 

fixed length stream formats, Object binding, filed validation, 150+ Java coding Q&As

integration with spring-batch, etc. Here is a basic tutorial to get
started.

Step 1: The sample CSV file to convert to an object of type Person. 300+ Big Data Interview
Q&As - Quick Prep FAQs
The first record is a header record, and the subsequent ones are
detail records. The file is person.csv under src/main/resources/data
300+ Big Data Interview
FAQs 📌 

250+ Scala Interview FAQs 


1 H, 2013-03-12
2 John,Smith, FAMILY 250+ Python interview
3 Peter,Smith, FAMILY FAQs 📌 
4 Gregory,Smith, FAMILY
5

Step 2: Define the dependency jar in pom.xml file. Key Areas & Companion
Techs FAQs

1 <!-- beanio --> 16+ Tech Key Areas FAQs 


2 <dependency>
3 <groupId>org.beanio</groupId> 10+ Companion Tech Q&As 
4 <artifactId>beanio</artifactId>
5 <version>2.0.2</version>
6 </dependency>
7

https://www.java-success.com/beanio-java-tutorial/ 1/7
06/12/2024, 19:21 BeanIO Java tutorial

800+ Enterprise & Core


Step 3:The main class that wires up everything. Java Q&As

300+ Core Java Q&As 


1 package com.mycompany.app10; 300+ Enterprise Java Q&As 
2
3 import java.io.InputStream;
4 import java.io.InputStreamReader;
5 import java.util.ArrayList;
6 import java.util.List; Java & Big Data Tutorials
7 import java.util.Map;
8
Tutorials - Golang 
9 import org.beanio.BeanReader;
10 import org.beanio.StreamFactory; Tutorials - Big Data 
11
12 public class BeanIoMain Tutorials - Enterprise Java 
13 {
14 public void readCSVFileUsingBeanIo()
15 {
16 StreamFactory factory = StreamFactory.newInstance();
17 factory.loadResource("person.xml");
18
19 //read it from the classpath : src/main/resources
20 InputStream in = this.getClass().getResourceAsStream(
21 BeanReader reader = factory.createReader("persons", n
22 Object record = null;
23 List<Person> persons = new ArrayList<Person>();
24
25 // read records from "input.csv"
26 while ((record = reader.read()) != null)
27 {
28 if ("header".equals(reader.getRecordName()))
29 {
30
31 @SuppressWarnings("unchecked")
32 Map<String, Object> header = (Map<String, Obj
33 System.out.println(header.get("fileDate"));
34 }
35 else if ("detail".equals(reader.getRecordName()))
36 {
37 Person person = (Person) record;
38 persons.add(person);
39 }
40 }
41
42 System.out.println(persons);
43 }
44
45 public static void main(String[] args)
46 {
47 new BeanIoMain().readCSVFileUsingBeanIo();
48 }
49 }
50

Step 3: The POJO Person class.

https://www.java-success.com/beanio-java-tutorial/ 2/7
06/12/2024, 19:21 BeanIO Java tutorial

1 package com.mycompany.app10;
2
3 public class Person
4 {
5 private String firstName;
6 private String surname;
7 private PersonType type;
8
9 public String getFirstName()
10 {
11 return firstName;
12 }
13
14 public void setFirstName(String firstName)
15 {
16 this.firstName = firstName;
17 }
18
19 public String getSurname()
20 {
21 return surname;
22 }
23
24 public void setSurname(String surname)
25 {
26 this.surname = surname;
27 }
28
29 public PersonType getType()
30 {
31 return type;
32 }
33
34 public void setType(PersonType type)
35 {
36 this.type = type;
37 }
38
39 @Override
40 public String toString()
41 {
42 return "Person [firstName=" + firstName + ", surname=
43 }
44
45 }
46
47

Step 4: The person.xml under src/main/resources where you can


map to your POJO Person.java.

1 <beanio xmlns="http://www.beanio.org/2012/03">
2 <stream name="persons" format="csv">
3 <parser>
4 <property name="delimiter" value="," />
5 <property name="lineSeparator" value="\n" />
6 <property name="whitespaceAllowed" value="true" />
https://www.java-success.com/beanio-java-tutorial/ 3/7
06/12/2024, 19:21 BeanIO Java tutorial
7 </parser>
8 <typeHandler name="personTypeHandler" class="com.mycompany
9 <record name="header" class="map" maxOccurs="1">
10 <!-- 'rid' indicates this field is used to identify the re
11 <field name="recordType" rid="true" literal="H" />
12 <!-- 'format' can be used to provide Date and Number forma
13 <field name="fileDate" type="date" format="yyyy-MM-dd" />
14 </record>
15 <record name="detail" minOccurs="0" maxOccurs="unbounded"
16 class="com.mycompany.app10.Person">
17 <field name="firstName" />
18 <field name="surname" />
19 <field name="type" typeHandler="personTypeHandler" type="c
20 </record>
21 </stream>
22 </beanio>
23

Step 5: Next, the handler class that parses Enums to String.

1 package com.mycompany.app10;
2
3 import org.beanio.types.TypeConversionException;
4 import org.beanio.types.TypeHandler;
5
6 public class PersonTypeHandler implements TypeHandler
7 {
8
9 public Object parse(String text) throws TypeConversionExc
10 {
11 PersonType personType = PersonType.valueOf(text.trim(
12 return personType;
13 }
14
15 public String format(Object value)
16 {
17 return value != null ? ((PersonType) value).name() :
18 }
19
20 public Class<?> getType()
21 {
22 return PersonType.class;
23 }
24 }
25

Step 6: “PersonType” enum class.

1 package com.mycompany.app10;
2
3 public enum PersonType {
4 MALE, FEMALE;
5 }

https://www.java-success.com/beanio-java-tutorial/ 4/7
06/12/2024, 19:21 BeanIO Java tutorial
6

Step 7: The output of the BeanIoMain is:

1
2 Tue Mar 12 00:00:00 EST 2013
3 [Person [firstName=John, surname=Smith, type=FAMILY], Person
4

‹ Understanding service and socket timeouts in Java enterprise applications

Fibonacci number with caching and Java 8 FP ›

About Latest Posts

Arulkumaran Kumaraswamipillai
Mechanical Engineer to self-taught Java engineer in 1999. Contracting since 2002 as a Java Engineer &
Architect and since 2016 as a Big Data Engineer & Architect. Preparation & key know-hows empowered me
to attend 150+ job interviews, choose from 130+ job offers & negotiate better contract rates. Author of the
book "Java/J2EE job interview companion", which sold 35K+ copies & superseded by this site with 3.5K+
registered users Amazon.com profile | Reviews | LinkedIn | LinkedIn Group | YouTube Email: java-
[email protected]

How to take the road less travelled?


Practicing & brushing up a few Q&As each day on broad number of main stream categories can make a huge impact in 3-12
months.

"You are paid to read & write lots of code & solve business problems in a
collaborative environment"
100+ Free Java Interview FAQs
100+ Free Big Data Interview FAQs

Don't be overwhelmed by the number of Q&As. Job interviews are not technical contests to see who gets most number of questions right.
Nobody knows everything. The Clarity of the answers you give with real-life examples will go a long way in getting you multiple job offers.
It pays to brush-up & choose from 2-6 job offers. Experienced interviewers can easily judge your real experience from a few open-ended
questions & the answers you provide.

If you are pressed for time, popular categories are pinned 📌


for you to prioritise based on the job description. Here are my top career
making know-hows & tips to open more doors as a Java/Big Data Engineer, Architect or Contractor.

1. Feeling stagnated?
2. How to earn more?
3. Freelancing Vs contracting?

https://www.java-success.com/beanio-java-tutorial/ 5/7
06/12/2024, 19:21 BeanIO Java tutorial
4. Self-taught professional?
5. Job Interview Tips
6. Resume Writing Tips

Top 12 popular posts last 30 days


300+ Java Interview Q&As with code, scenarios & FAQs
300+ Core Java Interview Q&As 01. Ice breaker Tell us about yourself? 02. Ice Breaker 8 Java real life scenarios…
(6,825)

300+ Big Data Interview Q&As with code, scenarios & FAQs
100+ SQL Interview Q&As 01. 50+ SQL scenarios based interview Q&As – What is wrong with this SQL code? 02.…
(2,425)

00: Top 50+ Core Java interview questions & answers for 1 to 3 years experience
Top 50 core Java interview questions covering core Java concepts with diagrams, code, examples, and scenarios. If you don't get…
(1,971)

Java 8 String streams and finding the first non repeated character with functional programming
Q1.Find the first non repeated character in a given string input using Java 8 or later? A1.Extends Find the first…
(1,669)

18 Java scenarios based interview Q&As for the experienced – Part 1


Let's look at scenarios or problem statements & how would you go about handling those scenarios in Java. These scenarios…
(1,541)

Membership Levels
Membership prices listed below are in Australian Dollars (A$). If you purchase in other currencies like Indian rupees, the equivalent…
(718)

30+ Java Code Review Checklist Items


This Java code review checklist is not only useful during code reviews, but also to answer an important Java job…
(524)

8 real life Java scenarios with Situation-Action-Result (i.e. SAR) technique


The SAR (Situation-Action-Result) technique is very useful to tackle open-ended questions like: 1. What were some of the challenges you…
(522)

01: 30+ Java architect interview questions & answers – Part 1


One of the very frequently asked open-ended interview questions for anyone experienced is: Can you describe the high-level architecture of…
(480)

15 Ice breaker interview Q&As asked 90% of the time


Most interviews start with these 15 open-ended questions. These are ice breaker interview questions with no right or wrong answers…
(412)

https://www.java-success.com/beanio-java-tutorial/ 6/7
06/12/2024, 19:21 BeanIO Java tutorial
0: 50+ SQL scenarios based interview Q&As – What is wrong with this SQL code?
This extends 18+ SQL best practices & optimisation interview Q&As. You can practice these SQLs by setting up the data…
(370)

02: 10 Java String class interview Q&As


Java Collection interview questions and answers and Java String class interview questions and answers are must know for any Java…
(357)

Disclaimer
The contents in this Java-Success are copyrighted and from EmpoweringTech pty ltd. The EmpoweringTech pty ltd has the right to correct or enhance the current content without

any prior notice. These are general advice only, and one needs to take his/her own circumstances into consideration. The EmpoweringTech pty ltd will not be held liable for any

damages caused or alleged to be caused either directly or indirectly by these materials and resources. Any trademarked names or labels used in this blog remain the property of

their respective trademark owners. Links to external sites do not imply endorsement of the linked-to sites. Privacy Policy

© 2024 800+ Big Data & Java Interview FAQs Responsive WordPress Theme powered by CyberChimps

https://www.java-success.com/beanio-java-tutorial/ 7/7

You might also like