• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Updating Properties file

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,
Need your help on the below.
Have a simple application that allows the user to register on the website.
Have a properties file that i need to store the username & password with which the user would register.

The code is as below :-
---------------------------------------
Properties properties.put(userId, userPassword);
String fileName = this.getClass().getClassLoader().getResource("resources/registeredUser.properties").getFile();

FileOutputStream op = new FileOutputStream(fileName); properties.store(op, null);
op.flush();
op.close();
---------------------------------------

I do not see the new User Id's and Passwords in the Properties file
nor do i see any exceptions.

Thanks in advance,
-Navi
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code itself seems ok (it does not compile, but I assume it's a copy/paste error)

Are you sure that you are looking at the correct properties file to verify the result?
For example when you would be using Eclipse, and the file is in a source folder, the modified file is in the (hidden) bin folder and not in your source folder.
 
Sheriff
Posts: 22773
130
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the problem is in your location. The getResource() method looks relative to your classpath, whereas the file is written relative to the current directory. If these are not the same you won't overwrite the file but create another one.

Please note that this approach is definitely not thread-safe. If two users register at the same time, both will try to write to the same file at the same time, with different contents.
 
Navi Lock
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys, thanks for the responses.
Both are correct.

Bart - I do see the updates in the bin folder.

Rob - Yes from a Threads perspective you are correct.

Thanks again,
-Navi.
 
Every plan is a little cooler if you have a blimp. And a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic