9

I know how to write text to a file and store it in Java, but I was wondering if there is a way of storing lots of variables that can be retrieved from the file at a later date, and put back into the Java application as variables.

I would be storing numbers and strings, but groups of them.

Thanks :)

3 Answers 3

14

Java can read properties files like it reads a map. This gives you an easy way of storing information using a key/value system.

Check out http://download.oracle.com/javase/6/docs/api/java/util/Properties.html

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

1 Comment

You're welcome @Humpheh. I used this a while back to store a bunch of data and it really beats basic I/O
5

Use XML! You will make your variables portable. Even your coffee machine will be able to use them.

A SAX parser is built in Java.

Here is a Java snippet for writing an XML file:

public void saveToXML(String xml) {
    Document dom;
    Element e = null;
    // get an instance of factory
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
        // using a factory get an instance of document builder
        DocumentBuilder db = dbf.newDocumentBuilder();
        // create an instance of DOM
        dom = db.newDocument();
        // create the root element
        Element rootElement = dom.createElement("myparameters");
        // create data elements and place them under root
        e = dom.createElement("brightness");
        e.appendChild(dom.createTextNode(brightness));
        rootElement.appendChild(e);

Here is a Java snippet for reading an XML file:

public boolean loadFromXML(String xml) {    
    Document dom;
    // get an instance of the factory
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
        // using factory get an instance of the document builder
        DocumentBuilder db = dbf.newDocumentBuilder();
        // parse using builder to get DOM representation of the XML file
        dom = db.parse(xml);
        Element doc = dom.getDocumentElement();
        // get the data 
        brightness = getTextValue(brightness, doc, "brightness");
        contrast = getTextValue(contrast, doc, "contrast");

4 Comments

Haha well that's not really my main use for this, but thanks ;)
This is still a good answer, XML is universal. But the properties file in my case worked best for me.
+1 for coffee machine (read: Java machine) reading Java variables
But really, use JAXB for something like this instead of parsing the code manually. WAY more work to write and if you change your class you have to change your read/write code also. Completely unnecessary in such a case (because I hope we can assume that the variables have some connection and are already stored in a class/collection)
0

Wel the best way to do this is to make an XML file. It can store any object in any sort of collection.

I guess the XML is the best way to keep it and retrieve it later on.

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.