Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Text file to Java Objects

 
Muralidharan Kandasamy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Example

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.

personal.txt
--------------
Murali MCA Denver
Simi BE LasAngels
Siddhu MBBS Washington


personal.java
-----------------
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
Posts: 11085
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[mg]edited out solution
@Harsha
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
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
put the data in a java properties files for example like
Object_1=Murali|MCA|enver
here data is separated by '|'
then read the file as an input stream using
getResourceAsStream()
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
Bartender
Pie
Posts: 10490
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".

Winston
 
Muralidharan Kandasamy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot for all your replies. I'm going to implement the simplest solution as you guys are suggesting.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic