• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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: 22701
129
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.
 
I RELEASE YOU! (for now .... ) Feel free to peruse this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic