I am reading data from a file and converting and writing it into a HashMap - key, value pair - and finally to write to a properties file. However i notice only the last value from the file is writing into the propertie file.not all the values. Can you please help me to write all the contents to the properties file.
The loop should contain only the p.put(...) call. Everything else should happen before or after the loop. Right now you're creating the same file over and over again, deleting the previous one in each loop iteration.
I understood and implemented it in my code. But the data is not getting written to the employeefile. I still shows the last line. where as in the console i am getting the output like this :After saving properties:{7902=7369,'SMITH','CLERK','17-DEC-80',800,NULL,20}
After saving properties:{7902=7369,'SMITH','CLERK','17-DEC-80',800,NULL,20, 7566=7788,'SCOTT','ANALYST','09-DEC-82',3000,NULL,20}
:
:
:
:
After saving properties:{7788=7876,'ADAMS','CLERK','12-JAN-83',1100,NULL,20, 7902=7369,'SMITH','CLERK','17-DEC-80',800,NULL,20, 7698=7521,'WARD','SALESMAN','22-FEB-81',1250,500,30, 7782=7934,'MILLER','CLERK','23-JAN-82',1300,NULL,10, 7566=7902,'FORD','ANALYST','3-DEC-81',3000,NULL,20, 7839=7566,'JONES','MANAGER','2-APR-81',2975,NULL,20, NULL=7839,'KING','PRESIDENT','17-NOV-81',5000,NULL,10}
It goes only till null value of the President. Not beyond that entry. Whereas there are still entries left.
Also, why are you splitting the line and then joining it back together? You can split the line on the comma and only split once by adding an argument to the split() call. Look among the mthods in the String documentation. I would suggest you use "\\s*,\\s*" as a regex instead of simply ",". That way you can strip whitespace along with the comma. If you want to remove commas from the output, consider the methods of String which replace text.
Don't call close on your readers and writers. There is no guarantee that you will actually close them; what if the preceding code causes an exception to be thrown? Use a finally to make sure it is closed, or try with resources is much easier.
I suspect there is an easier way to get properties from a Properties object into a Map; have you tried any addAll of similar methods of your Map?
Campbell Ritchie wrote:I suspect there is an easier way to get properties from a Properties object into a Map; have you tried any addAll of similar methods of your Map?
There is an easier way. Properties is a subclass of Hashtable<Object, Object> which implements Map<Object, Object>. So Properties is already a Map. (It isn't a Map<String, String>, it's true. But maybe that doesn't matter.)
Paul Clapham wrote:. . . So Properties is already a Map.
I thought there would be something like that.
(It isn't a Map<String, String>, it's true. But maybe that doesn't matter.)
Don't know; a Map<String, String> isn't a subclass of Map<Object, Object>; the signature of this method suggests it won't work like that. There should be another way round that, maybe by how the recipient Map is declared.
Well, if you have a Map<Object, Object> and you need a Map<String, String> then I'm pretty sure there's nothing built in which will cast the Objects to Strings automatically. As far as I know you'd have to explicitly write code which does those casts.
Post by:autobot
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
a bit of art, as a gift, the permaculture playing cards