• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Passing the contents of a file in a collection

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic