aspose file tools*
The moose likes Beginning Java and the fly likes Reading and Writing to properties file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading and Writing to properties file" Watch "Reading and Writing to properties file" New topic
Author

Reading and Writing to properties file

tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
I have a properties file i am using to store some information such as last upload date, location of the last used folder etc.
I have a stats panel which when the application is loaded retrieves the information from the property file and displays it, this works fine.
As my program runs, users select directories to upload files from, i want to store this changed information which will be the location folder and date and then display the updated stats.
The problem i have is when i set the properties with the new values it overwrites and removes previous entries which i don't want to change.

My properties file will start like this
numberOfLogs= 0
locationFolder=No Logs uploaded
uploadDate=No Logs uploaded

I am using the code below to update the properties file, but it removes all previous entries such as numberOfLogs and locationFolder.
Is there a way to just change one value in the properties file?

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7807
    
  21

tom davies wrote:The problem i have is when i set the properties with the new values it overwrites and removes previous entries which i don't want to change.

So, what do you want to happen in such a case? Is this supposed to be like a history file? If so, properties may not be the best way to go.

Otherwise; TellTheDetails (←click).

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
Winston Gutkowski wrote:
tom davies wrote:The problem i have is when i set the properties with the new values it overwrites and removes previous entries which i don't want to change.

So, what do you want to happen in such a case? Is this supposed to be like a history file? If so, properties may not be the best way to go.

Otherwise; TellTheDetails (←click).

Winston

Sorry, i will try and explain in a bit more detail.
My program allows users to upload, search and generate reports on a series of log files. The user can select a folder and set up an automated feature which will check their selected folder for any new files and automatically parse and store them.
I also have a stats panel which shows some key details such as the current number of logs stored, the current folder location and the date of the last log upload.

I want the stats panel to update as the user interacts with the program. When a new folder is selected and files are uploaded i want the folder location and date of last upload to update.
Currently i am trying to achieve this using the method above but it removes other entries which i have not specified new values for.
I would like to only update the values i specify and keep any others in the file intact.
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
Load the properties first, then update them, and then save them.
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
Emanuel Kadziela wrote:Load the properties first, then update them, and then save them.

That would still give me the same problem when i write back to the file.
There is no save method, only store.
The only way i can think of, still using the properties file is to read all entries, store them as variables, then update and store them all whenever there is a change.
I think there will be a maximum of 4 properties, its a solution but doesn't seem like the best.
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
Load the properties - that holds them in memory. If you want to update any of them, read what is there first, compare, update, and replace. Store whenever necessary (if you worry about crashes and stuff, store every time you make an update, otherwise, store periodically or just at the end of a session).

The alternative way is to use plain old file io with the option to append instead of overwrite. The problem there, is that you will end up with multiple properties with the same names (which is worse).
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
Emanuel Kadziela wrote:Load the properties - that holds them in memory. If you want to update any of them, read what is there first, compare, update, and replace. Store whenever necessary (if you worry about crashes and stuff, store every time you make an update, otherwise, store periodically or just at the end of a session).

The alternative way is to use plain old file io with the option to append instead of overwrite. The problem there, is that you will end up with multiple properties with the same names (which is worse).

hmmm, it looks as though it should work but for some reason it isnt.

In my constructor i am setting my properties variable using



If i do a println like this then i get the correct output in the console



But if i try and display it on my GUI like below. I get the values returning null

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7807
    
  21

tom davies wrote:But if i try and display it on my GUI like below. I get the values returning null

Are you sure that those statements are actually getting run AFTER you load your properties? They look like initialization statements to me.

However, that aside, I worry a bit about this whole business. It seems awfully clunky to me.

Winston
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
Winston Gutkowski wrote:
tom davies wrote:But if i try and display it on my GUI like below. I get the values returning null

Are you sure that those statements are actually getting run AFTER you load your properties? They look like initialization statements to me.

However, that aside, I worry a bit about this whole business. It seems awfully clunky to me.

Winston

Thanks, i cant believe i didn't spot that, i initialised all the panels first and then loaded the file in, i have swapped it around now and it displays the correct values.

What would you suggest as a better solution? The whole programs has a database packaged with it so i could always use that but it seems a lot of effort just for 3 or 4 simple values
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7807
    
  21

tom davies wrote:What would you suggest as a better solution? The whole programs has a database packaged with it so i could always use that but it seems a lot of effort just for 3 or 4 simple values

And if you were only reading them, I'd probably agree with you; but you seem to want them to be updated in real time as well - and for that, I'd say a database is far better, since it's precisely one of the things they were designed to do.

Winston

PS: I broke up that enormous line in your previous post. Please DontWriteLongLines (←click). Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading and Writing to properties file