0

Okay, Im working on a project that requires me to save a large volume of files. in the format .dat with information that the variable stores stored inside the file. Is there a way in Java 1.7 to create a variable with the name that it reads from file? If not what would be the best way to create an ever growing number of variables? Im at a bit of a loss, I don't know of a way to do what i need... Dpes anyone have any advice?

3
  • 3
    Dynamic variables names are not possible in Java. Use an array, List or Map. Commented Dec 5, 2013 at 4:48
  • In short words, Collection is your question's answer. Google it. Commented Dec 5, 2013 at 4:48
  • 1
    Why would you need a variable with a given name at runtime - its not like you can do extra manipulations? For an ever growing number of variables, as the others said use collections. Commented Dec 5, 2013 at 4:51

2 Answers 2

1

You can use any Map implementation like LinkedHashMap, HashMap or Hashtable. They all store objects classified by keys which have their corresponding values.

You would declare this object as:

Map myMap = new Hashtable<String, File>();

Do whatever implementation for file input, you can then use the put() method like:

myMap.put("fileName", fileInstance);

You can remove a key, check for a key existance, check for a value existance, iterate over the keys and over values.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use collections (i.e. List (java.util.List). You can also store the collection of your object in the file (I think use of serialize and de-serialize would increase your speed in reading and writing the data)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.