views:

64

answers:

5

Hi guys,

Im reading values from a file and storing these values in a hashmap, using a bufferedreader, in the following manner --

while((String str=buffread.readLine()).length()>1)
            {
                hashMap.put(str.substring(0,5),str);

            }

I can also verify that the hashmap has all data that was initially present in the file.

Now, Im trying to write the values of exact hashmap to another file in the following manner --

FileWriter outFile = new FileWriter("file path");
         PrintWriter out = new PrintWriter(outFile);
         Set entries = hashMap.entrySet();
          Iterator entryIter = entries.iterator();
          while (entryIter.hasNext()) {
             Map.Entry entry = (Map.Entry)entryIter.next();
             Object value = entry.getValue();  // Get the value.
             out.println(value.toString());
          }

But this seems to write lesser number of entries into the file than the value of hashMap1.size() or essentially, the number of entries that it initially read from the source file.

Though I have a hunch that its because of the Printwriter and filewriter, if anyone could point me to why this issue is occurring, it would be of great help.

Regards p1nG

A: 

You don't need the Iterator at this point, just use a keyset at iterate this

Set<String> keys = hashMap.keySet();
for(String key : keys){
 out.println(hashMap.get(key));
}

should do it.

asrijaal
Does that mean that the use of Iterator would might have caused the issue?
ping
This is inefficient, as you are 1) iterating over keys 2) then doing a lookup on the key. `entrySet()` is a more efficient way to iterate over the Map when you want to access both the `key` and the `value`. Unless you meant that one could just iterate over `values()`?
matt b
Well ping is only returning the entry values. So my guessing was this yes.
asrijaal
+2  A: 

Unless the first 5 characters on every line of your source file are unique, this line

hashMap.put(str.substring(0,5),str);

will ensure you're overwriting some entries in the Map.

Jonathon
Yes, They are unique in the source file.
ping
You absolutely certain about that? A sanity check that throws a warning on `hashMap.containsKey(str.substring(0,5))` would likely be worth adding.
BlairHippo
I am positive about that. They are essentially like private keys. Im confident that they're unique.
ping
+1  A: 

There is a possibility that something fails when writing to file:

Methods in this (PrintWriter) class never throw I/O exceptions. The client may inquire as to whether any errors have occurred by invoking checkError().

In general, I don't think it's a problem with HashMap, as you said that the data was read correctly.

m01
+2  A: 

Perhaps you left this out of the code you posted, but are you explicitly calling flush() and close() on the PrintWriter/FileWriter objects when you are done with them?

Each call to println() does not necessarily cause a line to be written to the underlying OutputStream/file.

matt b
My bad, silly silly silly :D
ping
A: 

You can't possibly read a file correctly with that code. You have to check the result of readLine() for null before you do anything else with it, unless you like catching NullPointerExceptions of course.

EJP