Forums Register Login

How to write all the data to a properties file

+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.

The code i refactored is :
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
That code still saves once per loop. Reread what I wrote earlier - all code related to storing should be outside the loop.
+Pie Number of slices to send: Send
 

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.)
+Pie Number of slices to send: Send
 

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 shou‍ld be another way round that, maybe by how the recipient Map is declared.
+Pie Number of slices to send: Send
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.
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
https://gardener-gift.com


reply
reply
This thread has been viewed 373 times.
Similar Threads
Storing data in Properties file and reading via HashMap
print the output of the console using JFrame
BufferedWriter, PrinterWriter etc
Read files and replace Strings
Default Properties
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:00:17.