首页 arrow J2ME开发 arrow 程序设计 arrow J2ME读取本地文本文件(多种编码)
J2ME读取本地文本文件(多种编码)
Author Author: Wupei | Date Date: 2009-10-31 | View Count View: 2711 | Section & Category J2ME开发 - 程序设计 | Digg Digg: 3
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;
   }
 
}

=======================================

//读取本地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编码:

 


更多阅读:

 
   评论 (2)
评论的 RSS
2
by: 刘忠鑫 (游客) 2011-12-30 09:30
为什么只是jar里的文本文件,没有读取本地文件? 
jsr75的那种读取
回复该评论
1
by: zz69 (游客) 2009-12-27 05:20
readChar没有提供类似String(byte[] , charset)这种构造char的功能,当构建器不知道你需要读取的char的编码格式的时候自然会报错了咯
回复该评论

我要发表评论

登录菜单

最新文章

本月热门

订阅本站

RSS 0.91 RSS 1.0 RSS 2.0 ATOM 0.3 OPML