Access to text files
To read the book, there is a book, it is impossible to think that the book is reflected in the form of String, that's too ..., huh, huh. Casually select the article with UltraEdit to store it as a Unicode encoding format, because Java in the ME impression is directly supporting Unicode.
In the MIDP in the impression, it seems that there is a function of accessing resources. Since IMGPATH can IMGPATE (IMGPATH); in the Image class: public static image createImage (String name) throws ioException The text can also pass Similar methods access. Looking for finding in DOCS, it is unaffir, the system supports access to resources, and automatically generates InputStream. The specific declarations are as follows: Class Class PUBLIC InputStream GetResource With a given name. This Method Returns Null IF No Resource with this name is found. The rules for searching resources is all profile specifics. If the file does not exist, return null; if it exists, open it is InputStream. Finding the rules of resources are defined in Profile, and the specific ME can't manage so much.
Since INPUTSTREAM is generated, you have to see what Java.IO package is going. InputStream is read processing in Byte, this efficiency is really low. DataInputStream can read the specified type, such as CHAR, INT, etc., can decode specific encoding, such as UTF, and other readchar read the character defaults to Unicode. Java is really a Unicode-based language, cool. Midlet used to test, 嘿嘿, no, read it is garbled. Where is the problem? ! View DOCS again, it's right, DataInputStream implements the DataInput interface, where readchar is the Unicode character read. Definition readChar in interface DataInput:.. Public char readChar () throws IOExceptionReads an input char and returns the char value A Unicode char is made up of two bytes Let a be the first byte read and b be the second byte The. Value Returned IS: ((a << 8) | (B & 0xFF) This method is suitable forreading bytes Written by The Writechar Method Of Interface DataOutput.returns: The Unicode Char Read. Do not die, try again, Still not! Buffle ... Is it not enough to master the interface and an abstract class? Don't you still have to inherit a DataInputStream manual to achieve readchar? No matter how much, try again, after all, the project has to continue to construct a DataInputStream object, one by one read byte, and then manually perform two bytes of continuous read according to the rules above. Test, output the correct Chinese characters. Blame, but no matter how strange, let's put it again. ? ? ? The test continues, discovers a problem, files stored in Unicode format, the first two BYTEs are FF Fe (hexadecimal), but also exist several file tests in the UE, as long as it is unicode encoding mode storage, head two Bytes are FF Fe. I don't know if I identify the file header of the Unicode encoding text file? !
Manual Test DataInputStream does not support Mark and Rewind. An error occurs, indicating that the InputStream generated in the J2ME is not supported by the INPUTSTREAM, which is not supported. Of course, RESET does not support. Fortunately, support Skipbytes, huh, huh, it is really big. Because the forward backward reading of the file must be implemented, the file pointer is required. Does not support reset, the rebind function has brought some obstacles, and these features are implemented later through other methods. Specifically, it is to define a FileLib class, which implements features read in a Windows system, such as reading bytes, reset, close, the key is to construct inputStream according to the builder parameter (file name), in obtaining the file In the size of the size, first record the current position, turn off the inputStream, reopen (this point in the file pointer in the file header), traversed to the end of the file (the result of the READ is negative 1), close, then open, SKIP to the original location . Very trouble, but the function is achieved. The sample code is as follows: / * Filelib.java * * Created on October 7, 2004, 1:21 pm / package heart5.lib;
Import Java.io.DataInputStream; Import Java.io.InputStream;
/ ** * fileLib, the library operates on the file; this file refers to the resource in the resource package * whether there is existence, size * @Author heart5 * @version start * / public class filelib {/ ** * InputStream Get input stream via getResource * / inputStream is; / ** * DataInputStream, data input stream, support readByte and other functions * Implement DataInput interface * / DataInputStream Dis; / ** * Data input stream pointer * / public int dispiDx = 0; / ** * file name, string * / string filename; / ** * builder generates this class * @Param FileName file name, string, string * / public filelib (String filename) {filename = filename; is = getClass (). getResourceAsStream (filename); dis = new databaseputStream (IS);} / ** * isok (), check if the file is normal * @return returns the cloth value, see if the file is ready * / Public boolean isok () {boolean ok = false; if (is! = Null) {ok = true;} return ok;} / ** * readchar (), read a value of a Unicode format * @return returns read UNICODE character, integer value * / public int ready () {INT CH = -1; try { Byte [] barray = new byte [2]; int pp = disp.read (barray); if (pp! = barray.length) || (pp == -1)) {Return Ch;} else {ch = (Barray [1] << 8) | (Barray [0] & 0xFF); disix = barray.length;}} catch (Exception E) {E.PrintStackTrace ();} returnch ch;} / ** * readstring (), Read the specified number of characters, return string * / public string readstring (int length) {StringBuffer strbuf = new stringbuffer (length); int TMP = readchar (); if (TMP =
= -1) {RETURN NULL;} for (int i = 0; (i