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
Perhaps you left this out of the code you posted, but are you explicitly calling
flush()andclose()on thePrintWriter/FileWriterobjects when you are done with them?Each call to
println()does not necessarily cause a line to be written to the underlying OutputStream/file.