| Author |
Updating Properties file
|
Navi Lock
Ranch Hand
Joined: Jul 29, 2003
Posts: 39
|
|
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
|
 |
bart zagers
Ranch Hand
Joined: Feb 05, 2003
Posts: 234
|
|
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.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Navi Lock
Ranch Hand
Joined: Jul 29, 2003
Posts: 39
|
|
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.
|
 |
 |
|
|
subject: Updating Properties file
|
|
|