有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java访问特定于平台的应用程序数据区

在Java中,我需要找到一种方法在本地存储一些数据,以便在重启之间可以使用。简单的事情,例如窗口位置/大小。我知道这些信息通常存储在Windows上的C:\Users\<username>\AppData中(通过在Windows资源管理器的地址栏中键入%APPDATA%来访问),但我不知道它存储在Mac OS X上的什么位置(顺便说一下,我仍然不了解Windows上的%APPDATA%文件夹的结构,以及它的\Roaming\Local\LocalLow等子文件夹)

当然,根据操作系统的不同,在这些区域中存储诸如窗口位置之类的信息是有办法的。也许甚至有一个图书馆已经为完成这些相对普遍的任务而预先构建好了


共 (3) 个答案

  1. # 1 楼答案

    您可以将数据存储在

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

    或者类似的东西。这应该适用于操作系统

  2. # 2 楼答案

    In Java, I need to find a way to store some data locally so that it is available between reboots. Simple things, such as window location/size.

    我认为Preferences API符合这一要求。简言之:

    Applications require preference and configuration data to adapt to the needs of different users and environments. The java.util.prefs package provides a way for applications to store and retrieve user and system preference and configuration data. The data is stored persistently in an implementation-dependent backing store. There are two separate trees of preference nodes, one for user preferences and one for system preferences.

    有一个简短但有用的教程here。下面是一个基于您需求的小示例:

    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();
                }
            });
        }
    }
    

    如果您第一次运行它,您将在console中看到首选项默认值:

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

    如果再次运行,则会更新这些值。就我而言:

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

    编辑

    根据下面的@Puce注释,Windows上的数据存储在注册表中,这是有意义的,因为这是Windows用于存储用户/系统/软件数据的方式。您可以在HKEY_CURRENT_USER\JavaSoft\Prefs\[package\class name]下找到示例中生成的注册表项,如下图所示:

    enter image description here

    注意:在Windows 8 x64、JDK 1.7上测试

    如果您不想用这些首选项填充注册表,那么您可以只存储应用程序文件夹的路径(与其他应用程序一样),并使用此路径从普通属性文件加载配置数据。继续使用首选项(至少用于存储应用程序的路径)的主要优点是,检索/存储配置数据的机制设计为跨平台的,JVM实现者(而不是开发人员)必须处理实际的实现

  3. # 3 楼答案

    抱歉,在标准Java库中没有解决方案

    您可能需要找到第三方库来实现这一点,或者使用其他解决方案(请参阅其他答案)