-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.

AXL
  • 17
  • 1
  • 6
  • 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](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Pshemo Feb 05 '15 at 16:57
  • `don't want to do it with file write/read` use database? But why do you want to do this? – Pradeep Simha Feb 05 '15 at 16:58
  • Related: [Keep persistent variables in memory between runs of Python script](http://stackoverflow.com/questions/6687660/keep-persistent-variables-in-memory-between-runs-of-python-script) – Christian Tapia Feb 05 '15 at 16:58
  • I don't know , thats why i am asking .. i don't want it to be modified easly – AXL Feb 05 '15 at 16:59
  • In "*then reuse it at the next program run*" do you mean other app, or also the same app (like after restarting it)? – Pshemo Feb 05 '15 at 17:02
  • Same app, after restarting it . – AXL Feb 05 '15 at 17:03
  • What exactly do you mean "modified easily"? If you're just worried about access, you can always encrypt the data when you persist it. – azurefrog Feb 05 '15 at 17:03
  • Yes, i am talking about the acces to it. – AXL Feb 05 '15 at 17:05
  • 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). – azurefrog Feb 05 '15 at 17:08

3 Answers3

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.

David Conrad
  • 15,432
  • 2
  • 42
  • 54
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").

David Conrad
  • 15,432
  • 2
  • 42
  • 54
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
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.

Christian Wilkie
  • 3,693
  • 5
  • 34
  • 49
  • 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)? – Pshemo Feb 05 '15 at 17:07
  • How would an in-memory database survive a restart? – JB Nizet Feb 05 '15 at 17:09
  • @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. – Christian Wilkie Feb 05 '15 at 17:10
  • @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. – Pshemo Feb 05 '15 at 17:11