Java Reference
In-Depth Information
public void savePref() throws IOException {
FileConnection fc = null;
DataOutputStream dos = null;
try {
// If exists already, first delete file, a little clumsy.
StringBuffer fileURL = new StringBuffer(mFileRoot);
fileURL.append(mDirectoryName);
fileURL.append(fileExt);
fc = (FileConnection) Connector.open( fileURL.toString(),
Connector.READ_WRITE);
if (fc.exists()) {
fc.delete();
fc.close();
fc = (FileConnection) Connector.open( fileURL.toString(),
Connector.READ_WRITE);
}
fc.create();
dos = new DataOutputStream(fc.openOutputStream());
// Now save the preferences records.
Enumeration keys = mHashtable.keys();
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
String value = get(key);
String pref = key + "|" + value;
dos.writeUTF(pref);
}
}
finally {
if (dos != null) {
dos.flush();
dos.close();
}
if (fc != null) fc.close();
}
}
}
Note the use of
FileSystemRegistry
to obtain the file system root in the constructor. The
first returned file root is used (usually
root1/
for the Wireless Toolkit).
Loading of data is done in the constructor, before the GUI becomes active. However, in the
MIDlet, saving of data is performed when handling a
Save
command. Since this is done within
the GUI event handling thread, you don't want it to hold it up. The
save()
method starts another
thread to perform the actual persistence.
Search WWH ::
Custom Search