J2ME读取本地文本文件(ANSI,Unicode,Unicode big endian,UTF-8编码)

来源:互联网 发布:女孩子英文名知乎 编辑:程序博客网 时间:2024/05/16 05:00

(源自: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编码:
原创粉丝点击