简体   繁体   English

Java是否具有类似于Cocoa的NSUserDefaults的东西?

[英]Does Java have something similar to Cocoa's NSUserDefaults?

Mac OS X and iOS have a nice little class called NSUserDefaults . Mac OS X和iOS有一个不错的小类NSUserDefaults It's a singleton that lets you store strings, arrays, and primitives, and you can always implement some methods to add custom objects to it. 它是一个单例,可让您存储字符串,数组和基元,并且始终可以实现一些方法来向其中添加自定义对象。 It's super useful when you need to store a quick setting without dealing with file manipulations (for example, storing the last picked font name). 当您需要存储快速设置而不处理文件操作(例如,存储最后选择的字体名称)时,此功能非常有用。

Does Java have something simple like this? Java有这样简单的东西吗? I'd like to be able to store a user's last settings to reload a similar state when the program reloads, but I'm not sure what the best way to do this is in Java. 我希望能够存储用户的上一次设置,以便在程序重新加载时重新加载类似的状态,但是我不确定用Java最好的方法是什么。

Yes, you can use the java.util.prefs API . 是的,您可以使用java.util.prefs API How do I save preference user settings in Java? 如何在Java中保存首选项用户设置? and What is the best way to save user settings in java application? 在Java应用程序中保存用户设置的最佳方法是什么? have some helpful info. 有一些有用的信息。 To get you started: 为了帮助您入门:

[[NSUserDefaults standardUserDefaults] setString:@"some string" forKey:@"some_key"];

becomes 变成

Preferences prefs = Preferences.userNodeForPackage(this);
prefs.put("some_key", "some string");

and

[[NSUserDefaults standardUserDefaults] stringForKey:@"some_key"];

becomes 变成

Preferences prefs = Preferences.userNodeForPackage(this);
prefs.get("some_key");

No. The closest thing is the property file system. 否。最接近的是属性文件系统。 You can have a property file (which is a key-value file). 您可以有一个属性文件(它是键值文件)。 Generally, they need to be in the classpath of the java program you're running. 通常,它们需要位于您正在运行的Java程序的类路径中。 You can also create property files (or xml files) anywhere you want to in the file system (like a known good place). 您还可以在文件系统中想要的任何位置(例如已知的好地方)创建属性文件(或xml文件)。 NSUserDefaults uses something like a p-list stored somewhere in the your home directory's Library, using a standard naming an directory path. NSUserDefaults使用标准目录命名方式,使用存储在主目录库中某处的p列表之类的东西。 You would have to come up with your own standard in Java. 您将不得不提出自己的Java标准。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java是否具有类似于c ++的可组合内容? - Does Java have something similar to c++'s combinable? .NET 是否有类似于 Azul 为 Java 提供的东西? - Does .NET have something similar to what Azul provides for Java? googles guava有java tryparse整数方法还是类似的? - Does googles guava have a java tryparse integer method or something similar? AS3 是否有与 Java 的 NumberFormat 类似的 class - Does AS3 have a similar class to Java's NumberFormat OkHttp是否具有类似于Unirest的用于创建RequestBody的field方法的更简单的东西? - Does OkHttp have something easier similar to Unirest's field method for creating a RequestBody? Java是否有类似于C ++标准库列表比较器的东西,用于通过变量值查找对象? - Does Java have something similar to C++ Standard Library list comparator for finding an object by a variable value? .NET 4.0是否具有类似于Java的容器管理事务的功能? - Does .NET 4.0 have something similar to Container-Managed Transactions from Java? 在java中有类似于array [1:]的东西吗? - Is there something similar to array[1:] in java? Java是否具有类似于Python的“ in”或“ not in”运算符? - Does Java have an “in” or “not in” operator similar to Python? c#中有什么类似于java的@override注释吗? - Is there something in c# similar to java's @override annotation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM