The java.util Package / java.util.prefs
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
This package make it possible to store and retrieve preferences, also called configurations. It's stored in such a way that it will persist from one invocation of your program to the next. The preference information can be stored in a variety of file types. It can be anything from a complex database arrangement to a flat file, but that doesn't matter to you. That's all automatic. The information is organized in hierarchical nodes. There are multiple collections of nodes. Each user can have his own collection of preferences. This normally includes things like fonts, colors and window sizes. All user share the same system values. This is information that can be the same for all users, like where the program files are located. The organization is like that of a file system, so a name may not be unique. You can tell them apart by where they're located in the tree. Also, the items in the tree are addressed by a path name. You can use both absolute and relative path names, just like with a file system. Absolute path names start with a slash character. You don't have to flush output to make sure the data persists. Normal termination of the JVM flushes the data to disk so you don't have to worry about it. This example returns the node of the user preferences for this particular application. This call to user node for package positions to the node by using the class object of this object. Actually, it's done this way to get the package name. Every application is made up of a group of classes, all of the same package. So the package name will be a unique identifier for the application. This example doesn't have a package name, so it uses the anonymous package, which works fine for development but it's not something you want to do in production software. If the name you choose is not already defined in the preferences, this method call will define it. Anyway, this example just prints out the name. Here's what happens when it runs. This is how the name of the anonymous package appears. If it had been a regular package name, it would have looked like the path name of a directory with slashes used for separators. This program also accesses the user node for the anonymous package. By the way, the system tree is accessed exactly the same way. You just replace the word user with the word system. Now, this example adds a preference entry. Every preference is in the form of a pair; a key and its value. The key is always a string. The value can be any one of several data types besides an int. It can be a string, long, float, double, Boolean or a byte or array. Each type has its own put and get method. There. This program should have added the value six for the number of bullets and the value should persist and be available to any other class of this package. This program should access the same node because it's being run by the same user and it's using the same key to retrieve the value. Notice that the method to retrieve the value has two arguments. The first one is the key, as you'd expect. The second one is the default value. If for any reason the key is not found, this method will return a default value of eight. Whatever value is returned, this method will then print it out. As you can see, the get method returns the value of six; the value that was stored in the preference by the other program. Remember, every user can have a different value for the same key and any user who has not defined a value will get the default. The next lesson is about using the timers that are available to you in the Swing package.
Tutorial Information
| Course: | Java 6 |
| Author: | Arthur Griffith |
| SKU: | 33858 |
| ISBN: | 1-934743-59-3 |
| Release Date: | 2008-02-29 |
| Duration: | 7 hrs / 92 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 