您当前位置:设计在线网 >> JavaScript >> 浏览文章

J2ME读取本地文本文件 UTF-8编码问题介绍

分享到:
本文章讲述了J2ME读取本地文本文件 UTF-8编码问题介绍.

2011年

01月01日

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;

}

} ================================================== ======================================

 

推荐阅读:
经验分享:对一个java初学者的建议
函数的实际参数-标识符arguments具有特殊含义
有关Java字符串编码知识介绍
推荐文章  
赞助商链接  
热门排行  
主题推广  
中国设计在线网 All Rights Reserved. 互联网违法和不良信息举报
信息产业部备案号:湘ICP备09001063号