File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes populating a linked list from text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "populating a linked list from text file" Watch "populating a linked list from text file" New topic

populating a linked list from text file

Shahabuddin Syed

Joined: Oct 25, 2003
Posts: 5
Hi, Ihave been working in java for quite some time but not on IO, if some one could pls help me out
my Query is :
A text file which has data from a table has to be read in the LinkedList were the width of the table has to be used as a tokenizer, this linked will be used later for a search operation.
thanks in Advance,
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Let's start way high up and look at the parts of the problem. You'll spot right off if I read the problem wrong ...

1a. Assuming your text field looks like "field1, field2, field3 \n" we can use a file reader and readln() to get a line.
1b. Parsing can be tricky, depending on what your data looks like. If you can trust a single delimiter to break up fields, a StringTokenizer will do the trick. But if you have classic Comma Separated Values for example, there might be commas inside quoted strings and you might go to regular expressions or something more sophisticated.
1c. Building an object is easy. Make a data-only object with a constructor that takes all the fields and populates member variables.
1d. Loading a collection is very interesting, and influenced by the requirements in part 2. If you retrieve objects by a single key, like PartNumber for a part, or SSN for a person, you might store the objects in a HashMap. If you retrieve by multiple keys, say SSN or LastName, you might put an object into TWO HashMaps - think of the extra map like an extra index on your database.
2a. Key retrieval. If you built 1d for this purpose, this is done.
2b. Retrieval by arbitrary fields. If you have to support squishy searches like "age > 40 and state of residence = CA" you'll have to iterate the collection and pick the ones that match. I'd start to think about leaving the data in the database!
Any of that help? BTW: There are some in-memory database projects around that do this kind of thing well. If it's one or two maps, writing your own is probably ok. If it's a bunch of them on a server with multiple users, I'd look into picking up some proven open source code.

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
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Oh, I didn't talk at all about linked list. Was that a requirement? Or just a general notion of how things might be stored?
It is sorta covered in the JavaRanch Style Guide.
subject: populating a linked list from text file
It's not a secret anymore!