代码之家  ›  专栏  ›  技术社区  ›  Viktor Sehr

Java是否提供某种寄存器“cookies”来存储临时数据?

  •  4
  • Viktor Sehr  · 技术社区  · 16 年前

    Java是否提供了一些注册表或cookie机制,在这里我可以存储小块数据来加载下一个开始,我启动Java应用程序还是applet?

    例如,应用程序设置,如上次打开的文件等

    2 回复  |  直到 16 年前
        1
  •  5
  •   dfa    16 年前

    Java属性在内存中作为持久性格式(文件)被广泛使用。它们也被诸如IDE、Ant、Maven等工具广泛支持。

    班级 Properties 它非常简单易用,有几种有用的方法(存储、加载和存储):

    Properties preferences = new Properties();
    preferences.put("color", "red");
    preferences.put("style", "bold");
    preferences.store(new FileOutputStream("prefs.properties"), "preferences");
    
    // reload the properties
    Properties preferences = new Properties();
    preferences.load(new FileInputStream("prefs.properties"));
    

    一个java.属性文件看起来像:

    # You are reading the ".properties" entry.
    ! The exclamation mark can also mark text as comments.
    website = http://en.wikipedia.org/
    language = English
    # The backslash below tells the application to continue reading
    # the value onto the next line.
    message = Welcome to \
              Wikipedia!
    # Add spaces to the key
    key\ with\ spaces = This is the value that could be looked up with the key "key with spaces".
    # Unicode
    tab : \u0009  
    
        2
  •  8
  •   Brian Agnew    16 年前

    Java Preferences API .

    它允许您存储首选项 每用户 每系统 . 它将把首选项存储在UNIX/Linux的隐藏文件中,并在基于Windows的系统中使用注册表(尽管这取决于实现)。

    注意:我不确定它是否适用于小程序(由于安全限制)。