• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to write all the data to a properties file

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Saloon Keeper
Posts: 7590
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sucheta Shrivastava
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 :
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Tim Moores
Saloon Keeper
Posts: 7590
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code still saves once per loop. Reread what I wrote earlier - all code related to storing should be outside the loop.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.)
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic