File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Updating Properties file Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Updating Properties file" Watch "Updating Properties file" New topic
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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Updating Properties file
 
Similar Threads
FTP problem with commons.net.ftp.FTPClient
FTP problem in commons.net.ftp.FTPClient
Accessing files that are there in classes directory
File copying
FTP problem in commons.net.ftp.FTPCient