aspose file tools*
The moose likes Beginning Java and the fly likes Passing the contents of a file in a collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing the contents of a file in a collection" Watch "Passing the contents of a file in a collection" New topic
Author

Passing the contents of a file in a collection

David Dickinson
Ranch Hand

Joined: Nov 11, 2004
Posts: 66
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

Joined: Jan 29, 2003
Posts: 8791
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
David Dickinson
Ranch Hand

Joined: Nov 11, 2004
Posts: 66
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

Joined: Jan 29, 2003
Posts: 8791
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing the contents of a file in a collection