Java Reference
In-Depth Information
Listing 9-1. A Preferences Class That Encapsulates File System Access
import java.util.*;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import java.io.*;
public class FileBasedPreferences implements Runnable {
private String mDirectoryName;
private static final String fileURLRoot = "file:///";
private static final String fileExt = ".pfs";
private Hashtable mHashtable;
private boolean mSaving = false;
private String mFileRoot = null;
public FileBasedPreferences(String dirName)
throws IOException {
mDirectoryName = dirName;
mHashtable = new Hashtable();
Enumeration em = FileSystemRegistry.listRoots();
// Take the first root available for simplicity.
if (em.hasMoreElements())
mFileRoot = fileURLRoot + em.nextElement();
if (mFileRoot != null)
load();
else
throw new IOException("No file system available");
}
public String get(String key) {
return (String)mHashtable.get(key);
}
public void put(String key, String value) {
if (value == null) value = "";
mHashtable.put(key, value);
}
private void load() throws IOException {
FileConnection fc = null;
DataInputStream dis = null;
Search WWH ::




Custom Search