• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Text file to Java Objects

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot for all your replies. I'm going to implement the simplest solution as you guys are suggesting.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic