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, regards, Shahabuddin.
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
Joined: Jan 29, 2003
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?