Hey guys, is it possible to create a properties file that maps an array to a string that can be read from? If so, how can I do this? What I need to do is be able to compare an array with that properties file to find the file source. However, when I tried creating this, reading it as a Properties file didn't work and I couldn't compare the arrays.
I'm thinking that a sample file might look something like this (please correct me if it should be some other way):
So if my current object had a String that was ["Toyota","Honda"], I should be able to get back "cars.txt" as a String value.
A sample class:
So I guess my main questions are:
1) How do I can create a method storeContents that writes to a properties file in the way I want?
2) How can I use an array as the key to get back my string value?
Why not just write a method which converts a String array to a String in that format? (If it doesn't already exist, that is.) Then you could use a Properties file which looks like that.
Joined: Jul 04, 2011
After a little bit of playing around and reading the docs, I found where my error was. I was artificially creating the Properties files thinking they would be like what I showed in my original post. However, it turns out that arrays as strings actually show up like this:
Here's my partially working code:
I need help with two other things...
1) FileInputStream throws an error if the properties file doesn't exist. How can I make sure to create a new properties file with just the one piece of data if it doesn't already exist?
2) What's the easiest/best way to sort the String for storing and checking? I want it so that the call to
prints out colors.txt instead of null. In order words, I don't care about the order of the items in the array, as long as they have the same contents.
Thanks Paul! Thumbs up. I didn't know about the sort function... I thought I had to convert it into a set and do all sorts of crazy stuff to convert back to an Array... Definitely saves me a lot of time.
Could you look over this code and see if it can be streamlined somehow? I'm always looking for improvement.
subject: Creating a text file that is a HashTable?