?? reader_menu.java
字號:
/**源代碼由程序員聯合開發網(www.pudn.com)會員"周潤發"收集、整理、重新編輯
*Email: ql_chuanzhang@tom.com
*QQ號:1103798882
*歡迎大家與我聯系互相交流學習
**/
package Reader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Form;
public class reader_menu extends Form {
private txt_reader txt_rd;
public reader_menu(txt_reader text_rd) {
super("多格式文本超級閱讀器");
this.txt_rd=txt_rd;
String strContent=myReadLine ("/files/text.txt"); //讀取text.txt文件中的內容返回字符串
String str_UTF8=read_UTF ("/files/UTF_8.txt"); //讀取UTF_8.txt文件中的內容返回字符串
String str_Unicode=read_Uni ("/files/Unicode.txt");
append("1.text\n"+strContent+"\n"); //將文本文件的內容加入到Form對象中
append("2.UTF8\n"+str_UTF8+"\n");
append("3.Unicode\n"+str_Unicode+"\n");
}
private String myReadLine (String testFileName) {
//函數頭定義了函數的參數為文件名組成的字符串,返回值為一個字符串。
InputStream in = this.getClass().getResourceAsStream(testFileName);
//由文件名參數來定義一個輸入流對象變量in
ByteArrayOutputStream s;
s = new ByteArrayOutputStream(); //產生內存數組輸出字節流對象變量S
try{
int ch=0;
ch=in.read(); //由輸入流對象變量in的read方法以字節為單位來讀取文件的內容,讀取到文件尾時的值為-1。
while ( ch!= -1 ) {
//如果未讀到文件尾把讀取的內容寫入S變量中,并讀取下一字節內容
s.write( ch );
ch=in.read();
}
in.close(); //關閉輸入流對象。
} catch (IOException ioe){
System.out.println(ioe.toString());
}
String str = s.toString(); //將S變量轉為字符串
try{
s.close(); //關閉輸出流對象
} catch (IOException ioe){
System.out.println(ioe.toString());
}
return str.trim(); //返回文件中的內容字符串
}
/**
* 讀取Unicode編碼文本文件
* @param resource String - 文件名
* @return String - Unicode文本
*/
public String read_Uni(String resource) {
byte word_uni[] = new byte[1024];
String strReturn = null;
InputStream is;
try {
is = this.getClass().getResourceAsStream(resource);
is.skip(2); // 跳過兩個字節的文件頭
is.read(word_uni);
is.close();
StringBuffer stringbuffer = new StringBuffer("");
for (int j = 0; j < word_uni.length; ) {
int l = word_uni[j++];
int h = word_uni[j++];
char c = (char) ((l & 0xff) | ((h << 8) & 0xff00));
stringbuffer.append(c);
}
strReturn = stringbuffer.toString();
} catch (IOException ex) {
System.out.println(ex);
} finally {
is = null;
}
return strReturn.trim();
}
/**
* 讀取UTF-8編碼文本文件
* @param resource String - 文件名
* @return String - UTF-8文本
*/
public String read_UTF(String resource) {
byte word_utf[] = new byte[1024];
String strReturn = null;
InputStream is;
try {
is = this.getClass().getResourceAsStream(resource);
is.read(word_utf);
is.close();
strReturn = new String(word_utf, "UTF-8");
} catch (IOException ex) {
System.out.println(ex);
}
return strReturn.trim();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -