J2ME read in a text file formats
Abstract: In the J2ME read the text files in various formats
Author: syngle Source: http://www.j2medev.com/Article/ShowArticle.asp?ArticleID=167
J2ME read in a text file formats
J2ME in the development process, we will be a regular text messages stored in txt format of the text in the document as a resource. So easy modification and management. Then read them to some friends Gangchachu j2me will be some difficulties. Few days before the study the next, the brothers read some articles and code, summed up three kinds of methods were read Unicode, UTF-8, the Unicode big endian format of this document did not consider … the efficiency of the reader.
These three methods can be read in English and Chinese characters. Used to store depending on the length of the array length of the text ….
Others can only read English characters do not give way out.
First, read Unicode format
Private String read_Uni (String resource)
(
Byte word_uni [] = new byte [1024];
String strReturn = "";
InputStream is;
Try
(
Is = getClass (). GetResourceAsStream (resource);
Is.read (word_uni);
Is.close ();
StringBuffer stringbuffer = new StringBuffer ("");
For (int j = 0; j <word_uni.length;)
(
Int k = word_uni [j + +]; / / attention in this place a system of code conversion
If (k <0)
K + = 256;
Int l = word_uni [j + +];
If (l <0)
L + = 256;
Char c = (char) (k + (l <<8)) / / number of the high and low assembled together
Stringbuffer.append (c);
)
StrReturn = stringbuffer.toString ();
)
Catch (IOException e)
(
E.printStackTrace ();
)
Finally
(
Is = null;
)
Return strReturn;
)
Second, read UTF-8 format
Public String read_UTF (String name)
(
String strReturn = "";
InputStream in = null;
Word_utf byte [] = new byte [1024];
Try
(
In = getClass (). GetResourceAsStream (name);
In.read (word_utf);
In.close ();
StrReturn = new String (word_utf, "UTF-8");
)
Catch (Exception e)
(
System.out.println ( "readUTF Error:" + e.toString ());
)
Finally
(
In = null;
)
Return strReturn;
)
Third, read Unicode big endian format
Read Unicode big endian format, using readChar () method read, store, use char array storage.
Note: in the text at the end of the '$' indicates the end of the text.
In addition to dis.skip code section 10 (2) is a document like the first two characters, if using microsoft notepad kept by the head of these two characters must exist.
Of course, you can use the UltraEdit two head could be deleted characters, then use the new document, copy paste, preserved for other formats. So no two characters of the first ..
Private String read_Uni_b_e (String resource)
(
Char word_uni_b_e [] = new char [1024];
String strReturn = "";
DataInputStream dis;
Try
(
Dis = new DataInputStream (getClass (). GetResourceAsStream (resource));
Int counter = 0;
Dis.skip (2);
Char temp;
While (true)
(
Temp = dis.readChar ();
If (temp =='$')
Break;
Word_uni_b_e [counter + +] = temp;
)
Dis.close ();
StrReturn = String.valueOf (word_uni_b_e, 0, counter);
)
Catch (Exception e)
(
System.out.println ( "read_Uni_b_e error!" + E.getMessage ());
)
Finally
(
Dis = null;
)
Return strReturn;
)
More than three kinds of method to test platform:
Operation System: Microsoft Windows XP Professional Service Pack 1
Emulator: Sun Wireless ToolKit 2.2 beta DefaultColorPhone
↑ Back
Tags: File






