aspose file tools*
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Text file to Java Objects" Watch "Text file to Java Objects" New topic
Author

Text file to Java Objects

Muralidharan Kandasamy
Greenhorn

Joined: Nov 10, 2011
Posts: 5
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

Joined: Jul 26, 2007
Posts: 10535
    
    9

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
@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

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
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

Joined: Mar 17, 2011
Posts: 8427
    
  23

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


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
Greenhorn

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: http://aspose.com/file-tools
 
subject: Text file to Java Objects