J2ME读取本地文本文件(ANSI,Unicode,Unicode big endian,UTF-8编码)
2010-09-15 16:45
381 查看
(源自:http://hi.baidu.com/wishwingliao/blog/item/220993dd184dab345982dd50.html)
import java.io.*; import javax.microedition.io.*; import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class LoadText extends MIDlet implements CommandListener { private Display display; private Form mainForm; private TextField textField; public void startApp() { this.display = Display.getDisplay(this); this.mainForm = new Form("本地ANSI).编码的文本"); this.textField = new TextField("靠自己","hello",10240,TextField.ANY); this.mainForm.addCommand(new Command("离开", Command.EXIT, 0)); String str = loadText("/kaoziji(ANSI).txt"); this.textField.setString(str); this.mainForm.append(this.textField); this.mainForm.setCommandListener(this); this.display.setCurrent(mainForm); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable s) { this.notifyDestroyed(); } //读取本地ANSI编码文本(没问题) private String loadText(String yourfilename) { InputStream is = null; String strs = ""; try { Class c = this.getClass(); is = c.getResourceAsStream(yourfilename); InputStreamReader isr = new InputStreamReader(is); StringBuffer buffer = new StringBuffer(); int ch; while ((ch = isr.read()) > -1) { buffer.append((char)ch); } strs = buffer.toString(); if (isr != null) isr.close(); if(buffer != null) buffer = null; } catch(IOException e) { } return strs; } } ======================================================================================== //读取Unicode编码的文本文件 (没问题) private String loadText(String resource) { byte[] word_uni = new byte[1024]; String strReturn = ""; InputStream is;//此抽象类是表示字节输入流的所有类的超类 try { is = this.getClass().getResourceAsStream(resource); is.read(word_uni);//从输入流中读取一定数量的字节并将其存储在缓冲区字节数组 b 中。 is.close(); StringBuffer stringbuffer = new StringBuffer(""); for(int j = 0; j < word_uni.length;) { int k = word_uni[j++];//这个地方进行了码制的转换 if(k < 0) k += 256; int l = word_uni[j++]; if(l < 0) l += 256; char c = (char)(k + (l <<8));//把高位和低位数组装起来 stringbuffer.append(c); } strReturn = stringbuffer.toString(); } catch(IOException e) { e.printStackTrace(); } finally { is = null; } return strReturn; } ======================================================================================== //读取本地UTF-8编码的文本文件(没问题) public String loadText(String name) { String strReturn = ""; InputStream in = null; byte[] word_utf= 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("读取本地UTF-8编码的文本出错:"+e.toString()); } finally { in = null; } return strReturn; } ======================================================================================== //读取本地Unicode big endian编码文本文件(没问题) public String loadText(String name) { String strReturn = ""; InputStream in = null; byte[] word_utf= new byte[1024]; try { in = getClass().getResourceAsStream(name); in.read(word_utf); in.close(); strReturn=new String(word_utf,"UTF-16BE");//后一个参数改为"UTF-8"即可读取UTF-8编码的 } catch(Exception e) { System.out.println(e.toString()); } finally { in = null; } return strReturn; } ======================================================================================== //读取Unicode big endian编码文本(有问题) private String loadText(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("读取Unicode big endian编码文本文件出错!" + e.getMessage()); } finally { dis = null; } return strReturn; } 读取Unicode big endian编码文本文件出错!null 不知何因 ======================================================================================== //读取ANSI编码文本 (有问题) private String loadText(String resource) { byte[] word_uni_b_e = new byte[10240]; DataInputStream dis; try { dis = new DataInputStream(getClass().getResourceAsStream(resource)); dis.readFully(word_uni_b_e); } catch(Exception e) { System.out.println("读取ANSI编码文本文件出错!" + e.getMessage()); } finally { dis = null; } return new String(word_uni_b_e); } ======================================================================================== 平台(Sun J2ME Wireless Toolkit2.2)下面是执行结果截图: ANSI编码: Unicode编码: UTF-8编码: Unicode Big Endian编码: |
相关文章推荐
- VC ANSI环境下按行读取ANSI、UNICODE 、UNICODE big endian、UTF-8四种文本文件
- 按行读取ANSI、UNICODE 、UNICODE big endian、UTF-8四种文本文
- [转载]Unicode、ANSI、UTF-8、Unicode Big Endian的故事
- UTF-8 ANSI Unicode Unicode big endian 乱码 文件格式 文件头
- 趣谈unicode,ansi,utf-8,unicode big endian这些编码有什么区别
- unicode,ansi,utf-8,unicode big endian这些编码有什么区别
- Unicode,ANSI,UTF-8,Unicode Big Endian的故事
- php 字符编码转换类,支持ANSI、Unicode、Unicode big endian、UTF-8、UTF-8+Bom 互相转换
- VC 编程ANSI环境下读写Unicode文件和将CStdioFile类扩展,读取UNICODE文本文件
- 趣谈unicode,ansi,utf-8,unicode big endian这些编码有什么区别
- 介绍一下unicode,ansi,utf-8,unicode big endian编码
- unicode,ansi,utf-8,unicode big endian的故事
- 趣谈unicode,ansi,utf-8,unicode big endian这些编码有什么区别(转载)
- ANSI、UTF-8、Unicode(little endian)、Unicode big endian
- VC 编程ANSI环境下读写Unicode文件和将CStdioFile类扩展,读取UNICODE文本文件
- Unicode,ANSI,UTF-8,Unicode Big Endian的故事
- VC 编程ANSI环境下读写Unicode文件和将CStdioFile类扩展,读取UNICODE文本文件
- unicode ansi utf-8 unicode_big_endian编码的区别
- 将CStdioFile类扩展,读取UNICODE和ANSI文本文件