-3

How can i save the value of a variable in my program and then reuse it at the next program run ? I don't want to do it with file write/read.

9
  • 6
    "I don't want to do it with file write/read" can we know the reason for this? Your question looks like XY problem Commented Feb 5, 2015 at 16:57
  • don't want to do it with file write/read use database? But why do you want to do this? Commented Feb 5, 2015 at 16:58
  • Related: Keep persistent variables in memory between runs of Python script Commented Feb 5, 2015 at 16:58
  • I don't know , thats why i am asking .. i don't want it to be modified easly Commented Feb 5, 2015 at 16:59
  • 3
    You should edit your question to clarify your problem then. I think @Pshemo hit it on the head that this is an XY problem. It sounds like what you want to know is how to persist data for reuse the next time your program runs, such that nothing but your program and read and/or alter that data. This has nothing to do with where you persist your data (i.e. in a file). Commented Feb 5, 2015 at 17:08

3 Answers 3

7

Use the Java Preferences API.

import java.util.prefs.*;

public class Example {
    // Preference key
    private static final String FRUIT = "fruit";

    public void savePreference(String favoriteFruit) {
        Preferences prefs = Preferences.userNodeForPackage(Example.class);

        prefs.put(FRUIT, favoriteFruit);
    }

    public String readPreference() {
        Preferences prefs = Preferences.userNodeForPackage(Example.class);

        return prefs.get(FRUIT, "default");
    }
}

The data is stored based on the fully-qualified name of your class, so your package name and class name are relevant. From the documentation for the Preferences class:

This class allows applications to store and retrieve user and system preference and configuration data. This data is stored persistently in an implementation-dependent backing store. Typical implementations include flat files, OS-specific registries, directory servers and SQL databases. The user of this class needn't be concerned with details of the backing store.

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

Comments

1

One can store settings using java.util.prefs.Preferences. For two target groups: normally user settings, and less often application/system settings. They can use the platform settings, like under Windows.

There exists however also the possibility to store the settings as XML, which is a cleaner way, as it does not touch the Windows registry, which might be protected. Then a customary location would be inside a directory like ".myapp" under the directory System.getProperty("user.home").

Comments

0

You can use an in-memory key-value store like Redis, or an in-memory database like H2. But this may be overkill depending on your needs.

4 Comments

Interesting idea, but will it also work when we restart our machine (just asking since I never used these tools and this case seems to be quite important)?
How would an in-memory database survive a restart?
@Pshemo it will not, since you are only writing it to memory and the memory will be erased when you restart the machine. However, you could host Redis on a different server that doesn't restart and write to that.
@JBNizet That is what I meant. But I assume that maybe it has mechanism which on restarts store data on disk and when machine is starting reads it to memory and deletes from disk.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.