Does Java have something similar to Cocoa's NSUserDefaults?

六眼飞鱼酱① 提交于 2019-12-04 10:10:51
Barry Wark

Yes, you can use the java.util.prefs API. How do I save preference user settings in Java? and What is the best way to save user settings in java application? 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. You can also create property files (or xml files) anywhere you want to in the file system (like a known good place). NSUserDefaults uses something like a p-list stored somewhere in the your home directory's Library, using a standard naming an directory path. You would have to come up with your own standard in Java.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!