Platform-specific application data area access

I am sure this question has been asked before, but I cannot find it. (Maybe I just don’t look at the right places.)

In Java, I need to find a way to locally store some data so that it is available between reboots. Simple things like window layout / size. I know that such information is usually stored in C:\Users\<username>\AppData on Windows (it is accessed by typing %APPDATA% in the address bar of Windows Explorer), but I have no idea where it is stored on Mac OS X. (As a note, I still do not understand the structure of this %APPDATA% folder on Windows with its subtexts \Roaming , \Local , \LocalLow , etc.)

Of course, there is some way to store information in the form of data such as the location of the window in these areas depending on the OS. Perhaps there is even a library already built to carry out these relatively ubiquitous tasks? Please inform. Thanks!

+5
source share
3 answers

In Java, I need to find a way to locally store some data so that it is available between reboots. Simple things like Location / Size window.

I think the settings API meets this requirement. In a nutshell:

Applications require preferences and configuration data to adapt to the needs of different users and environments. The java.util.prefs package provides the ability for applications to store and retrieve user and system preferences and configurations. Data is stored permanently in an implementation-dependent storage. There are two separate preferred node trees, one for user preferences and one for preference system.

Below is a short but useful tutorial here . And here is a small example based on your requirements:

 import java.util.prefs.Preferences; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.SwingUtilities; public class SwingPreferencesTest { private void createAndShowGUI() { JTextField dummyTextField = new JTextField(20); JFrame frame = new JFrame("Demo"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.add(dummyTextField); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); Preferences prefs = Preferences.userRoot().node(this.getClass().getName()); // Define default values here System.out.println("width: " + prefs.getDouble("width", 100d)); System.out.println("height: " + prefs.getDouble("height", 100d)); System.out.println("x: " + prefs.getDouble("x", 0d)); System.out.println("y: " + prefs.getDouble("y", 0d)); // Set new values here prefs.putDouble("width", frame.getPreferredSize().getWidth()); prefs.putDouble("height", frame.getPreferredSize().getHeight()); prefs.putDouble("x", frame.getLocationOnScreen().getX()); prefs.putDouble("y", frame.getLocationOnScreen().getY()); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new SwingPreferencesTest().createAndShowGUI(); } }); } } 

If you run it for the first time, you will see the default settings in the console:

 width: 100.0 height: 100.0 x: 0.0 y: 0.0 

If you run it again, these values ​​will be updated. In my case:

 width: 240.0 height: 59.0 x: 130.0 y: 130.0 

Edit

According to @Puce's comment below, Windows data is stored in the registry, and that makes sense because this is how Windows uses the user / system / software to store data. You can find the registry entry created in the example under HKEY_CURRENT_USER\JavaSoft\Prefs\[package\class name] , as shown in the figure below:

enter image description here

Note: tested on Windows 8 x64, JDK 1.7

If you do not want to fill out the registry with these settings, you can simply save the path to the folder of your application (as other applications do) and use this path to load configuration data from files with simple properties. The main advantage of using preferences (at least to save the path to applications) is the mechanism for receiving / storing configuration data intended for cross-platform ones, and JVM developers (and not developers) are people who have to deal with the actual implementation.

+2
source

You can save the data in

 Paths.get(System.getProperty("user.home")).resolve(".my-application-conf") 

or something similar. This should work through the OS.

0
source

AFAIK, there is no solution for the standard Java library.

You will either have to find a third-party library that does this, or use one of the alternative solutions (see other answers).

0
source

Source: https://habr.com/ru/post/1203799/


All Articles