Author: syngle Source: http://www.j2medev.com/Article/ShowArticle.asp? ArticleID = 167
Read text files in various formats in j2s
During the development of j2s, we often save a text information in a TXT text file as a resource. this facilitates modification and management. reading them will be difficult for some friends who have just started to get started with j2s. A few days ago I studied and read some of my brother's articles and code, summed up three methods to read Unicode, UTF-8, Unicode big endian format files... the read efficiency is not considered in this article.
These three methods can read Chinese and English characters. The length of the array to store depends on the text length ....
In addition, some methods that can only read English characters are not listed.
1. Read unicode format
Private string read_uni (string resource)
{
Byte word_uni [] = new byte [1, 1024];
String strreturn = "";
Inputstream is;
Try
{
Is = getclass (). getresourceasstream (Resource );
Is. Read (word_uni );
Is. Close ();
Stringbuffer = new stringbuffer ("");
For (Int J = 0; j <word_uni.length ;)
{
Int K = word_uni [J ++]; // note that code conversion is performed in this place.
If (k <0)
K + = 256;
Int L = word_uni [J ++];
If (L <0)
L + = 256;
Char c = (char) (K + (L <8); // assemble the high and low digits
Stringbuffer. append (C );
}
Strreturn = stringbuffer. tostring ();
}
Catch (ioexception E)
{
E. printstacktrace ();
}
Finally
{
Is = NULL;
}
Return strreturn;
}
2. Read the UTF-8 format
Public String read_utf (string name)
{
String strreturn = "";
Inputstream in = NULL;
Byte [] word_utf = new byte [1, 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;
}
3. Read Unicode big endian format
The readchar () method is used to read the Unicode big endian format. Therefore, the char array is used to store the data.
Note: Adding ''' $ ''' at the end of the text indicates the end of the text.
In addition, line 1 of the Code, dis. Skip (2), skips the first two characters of the file. If you use Microsoft notepad to save these two headers.
Of course, you can use ultraedit to delete the two headers first, then use the new file, copy and paste it, and save it as another format. In this way, the two headers are gone ..
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;
}
Test Platform for the above three methods:
Operation System: Microsoft Windows XP Professional Service Pack 1
Emulator: Sun wireless toolkit 2.2 beta defaultcolorphone