J2ME学习笔记(6)—连接MIDlet到文本文件

2016-02-19 13:36 0 1 收藏

在这个颜值当道,屌丝闪边的时代,拼不过颜值拼内涵,只有知识丰富才能提升一个人的内在气质和修养,所谓人丑就要多学习,今天图老师给大家分享J2ME学习笔记(6)—连接MIDlet到文本文件,希望可以对大家能有小小的帮助。

【 tulaoshi.com - 编程语言 】

1. J2ME中的连接类
  1) J2ME中,网络连接由类属连接框架(Generic Connection Framework)(GCF)处理,它是一组API,它有一个类和八个接口。GCF驻留在Javax.microedition.io包中。
  
  2) CGF的优点:
  
  增加了支持不同种类网络协议的一致性;
  
  定义和使用更可靠和可扩充的新协议;
  
  增加与标准Java技术中类库的兼容性。
  
  3) GCF包括
  
  类(Connector)
  
  异常(ConnectionNotFoundException)
  
  接口(Connection,DatagramConnection,StreamConnectionNotifier,InputConnection,InputConnection,StreamConnection,ContentConnection)
  
  4) J2ME中用microedition.io包替代J2SE中java.net包。
  
  2. J2ME中的I/O类
  1) J2ME中I/O类库由java.io包支持
  
  2) 例如使用Reader和Writer类处理字符流
  
    使用InputerStream和OutputStream类处理字节流
  
  3. 实例:
  1) 任务陈述:SaveMyMoney银行应用程序需要对存储在J2EE服务器上的文本文件进行检索,并在手机屏幕上随机显示文本中某一行的内容
  
  2) 开发步骤:
  
  a.打开记事本,写如如下代码:
  
  The keyWord to know the current balance is SMMBCBAL.
  
  The keyword to know the check status is SMMBCHKS.
  
  The keyword to oBTain mini statement is SMMBMINI
  
  The keyword to know the fixed deposit details is SMMBFDDT.
  
  The keyword to request for checkbook is SMMBBOOK.
  
  The keyword to stop check transaction is SMMBSTOP.
  
  The keyword to request for bill presentation is SMMBBILL.
  
  The keyword to request for help is SMMBHELP.
  
  保存为keyword.txt,并把该文件放入J2EE服务器的public_Html文件夹中,作为SaveMyMoney银行应用程序检索的对象。(前提是你必须安装J2EE服务器,你可以查看J2EE相关资料)。
  
  b.编写代码,如下:
  
  import javax.microedition.midlet.*;
  
  import javax.microedition.lcdui.*;
  
  import java.io.*;
  
  //javax.microedition.io包包含用来把MIDlet连接到网络资源上所要使用的类和接口,
  
  //假如你要建立MIDlet和文本文件之间的双向连接,你可以使用StreamConnection接口。
  
  //你可用HTTP连接来检索储存在J2EE服务器中的文本文件的数据
  
  import javax.microedition.io.*;
  
  import java.util.*;
  
   public class SaveMyMoney extends MIDlet implements
  
  CommandListener
  
   {
  
    private Command exitCommand, nextCommand;
  
    private Display display;
  
    private Form form;
  
    private StringItem keyWord;
  
    private Vector keyVector;
  
    public SaveMyMoney()
  
    {
  
  display = Display.getDisplay(this);
  
  exitCommand = new Command("Exit",Command.EXIT,2);
  
  nextCommand = new Command("Next",Command.OK,2);
  
  form = new Form("SMMB KEYWORDS HELP");
  
  keyWord = new StringItem(" ","we help");
  
  Ticker ticker = new Ticker("want to know your balance, check status ,transaction details, or bill details?use these keywords to bank with us");
  
  form.setTicker(ticker);
  
  form.append(keyWord);
  
  form.addCommand(exitCommand);
  
  form.addCommand(nextCommand);
  
  form.setCommandListener(this);
  
  keyVector = new Vector();
  
    }
  
   public void startApp() throws MIDletStateChangeException
  
   {
  
    display.setCurrent(form);
  
    readKeyword();
  
    showKeyword();
  
   }
  
   public void pauseApp(){}
  
   public void destroyApp(boolean unconditional){}
  
   public void commandAction(Command c, Displayable d)
  
   {
  
    if(c==exitCommand)
  
    {
  
  destroyApp(false);
  
  notifyDestroyed();
  
    }
  
    else if(c==nextCommand)
  
    {
  
  showKeyword();
  
    }
  
   }
  
   private void readKeyword()
  
   {
  
    StreamConnection connect = null;
  
    //创建输入流以检索连接中的数据
  
    InputStream inStream = null;
  
    //创建一个存储被检索数据的串缓冲区
  
    StringBuffer buffer = new StringBuffer();
  
    try
  
   {
  
   //建立HTTP与存储在J2EE服务器中的keyword.txt文件进行连接
  
   //Connection对象被设置为StreamConnection类型,以便输入和输出流可通过连接发送.
  
    connect = (StreamConnection)Connector.open("http://localhost:8000/keyword.txt");
  
    //openInputStream方法打开连接的输入流
  
    inStream = connect.openInputStream();
  
    int input;
  
    //用read()方法检索数据,返回-1时到达文本文件末尾,while循环终止
  
    while ((input=inStream.read())!= -1)
  
    {
  
   //缓冲区一次存储一行文本
  
  if (input!='')
  
  {
  
   buffer.append((char)input);
  
  }
  
  else
  
  {
  
  //文本被传递到向量keyVector
  
   keyVector.addElement(buffer.toString());
  
   buffer = new StringBuffer();
  
  }
  
    }
  
   }
  
    catch(IOException e)
  
    {
  
  System.err.println(" the connection could not be established. sorry for the inconvenience");
  
    }
  
   }
  
   private void showKeyword()
  
   {
  
    //随机地从向量中选择一行文本,把此行存储在称为keyword的StringItem对象中,然后在手机屏幕上显示此行
  
  Random random = new Random(Calendar.getInstance().getTime().getTime());
  
  int position = Math.abs(random.nextInt()) % keyVector.size();
  
  keyWord.setText((String)keyVector.elementAt(position));
  
   }
  
  }
  
  c.运行J2EE服务器,在命令提示符下打入命令j2ee –verbose
  
  d.打开Ktoolbar,新建项目----点击Build进行编译,预检验和打包----点击Run进行测试

来源:https://www.tulaoshi.com/n/20160219/1604759.html

延伸阅读
Java的IO操作都是基于流进行操作的,为了提高读写效率一般需要进行缓冲。     简单的示例程序如下:         /**  * 读出1.txt中的内容,写入2.txt中  *  */ import java.io.*; public class ReadWriteFile{  public static void main(String[] args){   try{ &nbs...
随着PDA和手机用户越来越倾向将自己信息存储在自己设备中。对于一个Java程序开发者来说,这意味着他必须学习怎么编写手机和PDA这些轻便设备的代码。近来一些研究表明Java在快速增长的手机和无线AD市场中占有重要地位。此外,到2004年将统治这些设备开发平台。 让我们看看J2ME的语言规格,通过这种方式你可能会对开发这种平台产生浓厚...
标签: Web开发
首先,我将讨论一下HttpConnection接口,这个接口可以用来建立Http连接 HttpConnection 接口 Connected Limited Device Configuration(有限连接设备配置。简称CLDC)。提供了一套用于网络连接的类,就是普通连接框架?一种平台独立连接框架,提供了一种分层的连接接口,它的实现操作系统由具体的设备简表提供(比如Mobile Inf...
在MIDlet程序学习中,生命周期是一个比较抽象的概念。其实生命周期就是一个简单的规定,规定了MIDlet中的每个方法,什么时候被系统调用。下面是一个示例代码,在每个方法的内部都输出一条语句,可以根据程序的输出结果来验证各方法被调用的顺序,具体代码如下: !-- frame contents -- !-- /frame contents -- //文件名...
随着移动通信的突飞猛进,移动开发这个新鲜的字眼慢慢成为开发者关注的热点。在网上进行的最近一份调查显示,有24.34%的受访者涉足嵌入式/移动设备应用开发,这个数字可能略高于实际的比例,但也足可说明嵌入式/移动设备应用开发是一块诱人的新鲜奶酪。 !-- frame contents -- !-- /frame contents -- J2ME(Java 2 Micro Edition)...

经验教程

621

收藏

38
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部