- 注册时间
- 2007-9-10
- 最后登录
- 1970-1-1
|
在手机中,可以存放TXT文件或支持TXT文件,我们可以利用J2ME中的流进行操作,把TXT文件中内容读出来!对于内容相对固定的应用,这是一个不错的方案!原与高手更新一步研究可在我的个人网站上留言http://wcp.fh668.com,代码如下:
package screen;
import javax.microedition.lcdui.*;
import java.io.*;
public class TextBoxTest extends Form implements CommandListener{
private Display display;
private Displayable parent;
private Command back,ok;
private TextBox tb;
private String txt;
private String fileName;
public TextBoxTest(Display d,Displayable p,String f)
{
super("TextBox演示程序");
display=d;
parent=p;
fileName=f;
back=new Command("返回菜单",Command.BACK,1);
ok=new Command("开始演示",Command.SCREEN,2);
txt=read_UTF(fileName);
tb=new TextBox("读取TXT文件",txt,txt.length(),TextField.ANY);
addCommand(back);
tb.addCommand(back);
addCommand(ok);
setCommandListener(this);
tb.setCommandListener(this);
}
public void commandAction(Command action,Displayable dpa){
if(action==back){
display.setCurrent(parent);
}
else if(action==ok){
display.setCurrent(tb);
}
}
public String read_UTF(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("readUTF Error:"+e.toString());
}
finally
{
in = null;
}
return strReturn;
}
} |
|