The java.io package in the CLDC/MIDP world is a stripped down version of java.io in J2SE. Table 4-2 summarizes the classes of java.io in both J2SE and CLDC/MIDP. As you can see, many of the java.io classes you normally find in J2SE are missing from CLDC/MIDP.
Table 4-2: The java.io Package
INTERFACES | |
---|---|
J2SE | MIDP |
DataInput | DataInput |
DataOutput | DataOutput |
Externalizable | - |
FileFilter | - |
FilenameFilter | - |
ObjectInput | - |
ObjectInputValidation | - |
ObjectOutput | - |
ObjectStreamConstants | - |
Serializable | - |
CLASSES | |
---|---|
J2SE | MIDP |
BufferedInputStream | - |
BufferedOutputStream | - |
BufferedReader | - |
BufferedWriter | - |
ByteArrayInputStream | ByteArrayInputStream |
ByteArrayOutputStream | ByteArrayOutputStream |
CharArrayReader | - |
CharArrayWriter | - |
DataInputStream | DataInputStream |
DataOutputStream | DataOutputStream |
File | - |
FileDescriptor | - |
FileInputStream | - |
FileOutputStream | - |
FilePermission | - |
FileReader | - |
FileWriter | - |
FilterInputStream | - |
FilterOutputStream | - |
FilterReader | - |
FilterWriter | - |
InputStream | InputStream |
InputStreamReader | InputStreamReader |
LineNumberInputStream | - |
LineNumberReader | - |
ObjectInputStream | - |
ObjectInputStream.GetField | - |
ObjectOutputStream | - |
ObjectOutputStream.PutField | - |
ObjectStreamClass | - |
ObjectStreamField | - |
OutputStream | OutputStream |
OutputStreamWriter | OutputStreamWriter |
PipedInputStream | - |
PipedOutputStream | - |
PipedReader | - |
PipedWriter | - |
PrintStream | PrintStream |
PrintWriter | - |
PushbackInputStream | - |
PushbackReader | - |
RandomAccessFile | - |
Reader | Reader |
SequenceInputStream | - |
SerializablePermission | - |
StreamTokenizer | - |
StringBufferInputStream | - |
StringReader | - |
StringWriter | - |
Writer | Writer |
Although the differences between the J2SE and CLDC classes appear large, they can be easily grouped into three categories:
As CLDC/MIDP has no conception of a local file system, all the classes having to do with files have been pruned from the java.io package. This includes File, FileInputStream, FileOutputStream, the corresponding Reader and Writer classes, RandomAccessFile, and various supporting classes. If you need to store data persistently on a device, you'll need to use the javax.microedition.rms package API, described in Chapter 7.
Object serialization is not supported in CLDC. This means that the Serializable interface and various object stream classes are not present.
Finally, J2SE includes a handful of utility stream classes-things you might want someday but shouldn't include on a device with a small amount of memory. These classes include piped streams, pushback streams, sequence streams, line numbering streams, and a few other gems like StreamTokenizer. If you really need one of these in your MIDlet, you can always package it with your application.[1]
MIDP includes the Reader and Writer character streams for working with Unicode characters. InputStreamReader and OutputStreamWriter handle the conversion between byte streams and character streams, just as in J2SE. An encoding determines how translation occurs between byte streams and character streams. A default encoding is used if you don't specify one. You can pass an encoding name to the constructors for InputStreamReader and OutputStreamWriter, if you wish. So far, this is all the same as in J2SE. In MIDP, though, you will likely find many fewer available encodings than in J2SE.
The default encoding for a MIDP implementation can be obtained by calling System.getProperty("microedition.encoding")-by default, ISO8859_1.
As described in Chapter 7, you can retrieve resource files from your MIDlet suite's JAR file. Use the getResourceAsStream() method in Class; it returns an InputStream that you can use as you please.
[1]A better idea would be to redesign your application so that complicated stream processing isn't necessary on the MID. In general, you should make your server do as much work as possible and your MIDlet do as little as possible.
Team-Fly |