wood burning stoves*
The moose likes Beginning Java and the fly likes Creating a text file that is a HashTable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating a text file that is a HashTable?" Watch "Creating a text file that is a HashTable?" New topic
Author

Creating a text file that is a HashTable?

Justin Filmer
Greenhorn

Joined: Jul 04, 2011
Posts: 27
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jul 04, 2011
Posts: 27
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

(1) The java.io.File class has an exists() method.

(2) Arrays.sort(String[]).
Justin Filmer
Greenhorn

Joined: Jul 04, 2011
Posts: 27
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.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Creating a text file that is a HashTable?
 
Similar Threads
Question about instanceof
Constructing multidimensional arrays
Updated MulitThreads Problem
comparing using arrays in java
comparing and sorting arraylist of arraylist