This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Collection to read data from a file and store it in memory??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

Collection to read data from a file and store it in memory???

Sneha Kashyap
Greenhorn

Joined: May 06, 2009
Posts: 22
Hi,

data in the file(will be read into a Set) (I am not exactly sure as to which collection to be used for this purpose) Please suggest the collection to be used.

abc def ghi
abc ghi
def ghi abc
uvw xyz
xyz abc
xyz def

How to read each line in this file and store it in a collection and also help me as to which collection can be used.
Please provide some sample code for the same
Thanks.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1446
    
    6

As far as reading is concerned you can have a look into java.io or java.util.Scanner, and as far storing in collection is concerned probably you won't be able to store it into hashmap or hashtable because as the sample data looks it doesn't seem to have any unique key, others should be fine and usage depends upon your requirement.


Swastik
Sneha Kashyap
Greenhorn

Joined: May 06, 2009
Posts: 22
Hi thanks,

would be great if you could helpme with some sample code....
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41019
    
  43
This is essentially the same question as this one and this one, in both of which you've received several answers. If you have questions about those, please continue the discussion in one of those topics; I'm closing this one.


Ping & DNS - my free Android networking tools app
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Sneha Kashyap wrote:would be great if you could helpme with some sample code....

Here on JavaRanch we love to help people learn Java, but we don't want to give out complete solutions, because that doesn't help people learn Java.

Do you know how to read a file line by line? It's easy to do with a BufferedReader object, which has a readLine() method to read one line from a file. Write a loop that keeps calling readLine() until it returns null (which means you've reached the end of the file). Instead of BufferedReader you could also use class Scanner; you'd have to use the methods hasNextLine() and nextLine() from that class. Lookup the API documentation of those classes and try out how they work.

What collection you should use depends on what the purpose of the program is. If you just need to store all the lines, then a List (ArrayList or LinkedList) would be an obvious choice. But you're mentioning a Set in your question.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collection to read data from a file and store it in memory???
 
Similar Threads
Help with Data Structures...
'&' missing while displaying Textbox values in JSP/jquery/javascript
use of '\f' ?
Collection in java
Concurrent Modifiaction Exception