This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Text file to Java Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Text file to Java Objects" Watch "Text file to Java Objects" New topic

Text file to Java Objects

Muralidharan Kandasamy

Joined: Nov 10, 2011
Posts: 5

Is there any kind of framework/tool/solution to convert the text file content into java pojo objects.


I have a text file containing 100 lines of data, each line is having 5 columns of data, each column I want to assign to my java pojo class attributes. So each line of record is represending a pojo class. At the end of my process, I should have a list of pojo class for the entire file.

Murali MCA Denver
Simi BE LasAngels
Siddhu MBBS Washington
class personal {
String name;
String degree;
String location;

In my java code, it should take the input of the personal.txt file and conver that into a list of personal pojo objects (List<Personal> personalList) as a output. How do we achieve this??

Please suggese me the solution as earliest, thanks in advance.

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10171

Welcome to the Ranch.

Define a constructor which will accept the required values. Inside the constructor, map these values to the appropriate variables.
Read a line from the file, parse it, and use the constructor to create an instance.
Add the created instance to the list.

PS. It should be Personal and not personal. As per java naming conventions, class names should be capitalized.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
[mg]edited out solution
Please do not provide ready made solutions.
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
1.Create "Personal" Java Bean (setters and getters for every instance variable, a zero argument constructor(default constructor))

2. Read the file using scanner. for each line, create a "Personal" Object and split the line using the column delimiter. set the attributes of the objects by using the elements of the array produced by split.

3. Add the object to the List<Personal> object that you created before reading the file.
Nomaan Butt
Ranch Hand

Joined: Oct 19, 2011
Posts: 54
put the data in a java properties files for example like
here data is separated by '|'
then read the file as an input stream using
then retrieve the data using

split the data retrevied from the file using split method like this

then initiate the Personal class objects and put them in ArrayList

Hope this will help
Winston Gutkowski

Joined: Mar 17, 2011
Posts: 7552

Muralidharan Kandasamy wrote:Is there any kind of framework/tool/solution to convert the text file content into java pojo objects.

In answer to your original question: no, there is no specific tool or framework for what you want unless, as Nomaan suggested, you're willing to conform to a specific set of formatting rules for your text. All the answers provided are techniques for doing what you want, but there is no "silver bullet".


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Muralidharan Kandasamy

Joined: Nov 10, 2011
Posts: 5
Thanks alot for all your replies. I'm going to implement the simplest solution as you guys are suggesting.
I agree. Here's the link:
subject: Text file to Java Objects
Similar Threads
How to read a formatted text file into objects
Deleted Flag
Publishing the message serially to the listener
How to return data from a database table using Apache CXF web service?
Maintaining File Size while writing