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.
Joined: Jan 29, 2003
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.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Nov 11, 2004
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?
Joined: Jan 29, 2003
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.