Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing the contents of a file in a collection

 
David Dickinson
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a class which reads the contents of a file and passes back the contents to its calling method line by line.

Now i'm looking to enhance this and pass the whole file, I want to do this by storing it within a collection.

I need some advice as to which is the best collection for this purpose.

The data is stored with whitespace as delimiters and each line has following tokens which only mean anything when together.

E.g. a spreadsheet style format..

Name, Age, Course

Thanks very much.
So far I have my program working fine passing it line by line but I would like to make my code more efficent.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like a great opportunity for a tiny custom class.

Now you can do something like

Now I finally got to your question with "collection". As a generic term it means a holder for a bunch of things. I loved the name "bag" in SmallTalk. In Java Collection is an interface with a number of cool implementations. Look at the JavaDoc for Collection, List, ArrayList and see if you find something you like. You can return any of the concrete implementations from your method.

I'm also leaving for you to puzzle out how the calling method gets your things out of the collection and uses them. Write again if you need some hints there.
 
David Dickinson
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan,

Thanks for your post, I had already implemented my custom classes which hold the data that is processed from the file, this is working fine.

My primary concern atm is I want to make the file processing and object instantiation code more efficent.

At the moment one of my classes reads the file and passes each line seperately to the class which creates the objects.
I presumed (maybe incorrectly) that it would be more efficent to read the file and add each line (hopefully pre-tokenized) into a collection which could then be passed as a complete collection to the object instantiating class.

Is this not a good way of doing it?

Thanks
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't worry too much about the efficiency; either way will be fine. If we were going to have readers for multiple sources that generate the same little objects - flat file, properties file, XML parser, database, remote procedure call, etc - then I'd spend some time studying the responsibilities and avoiding duplicate code in all the readers. But don't try to solve that problem until it happens.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic