• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating a text file that is a HashTable?  RSS feed

 
Justin Filmer
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

THANKS SO MUCH for the help!
 
Paul Clapham
Sheriff
Posts: 22521
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Justin Filmer
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Clapham
Sheriff
Posts: 22521
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(1) The java.io.File class has an exists() method.

(2) Arrays.sort(String[]).
 
Justin Filmer
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!