网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > J2EE/J2ME
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.Servlet中jdbc应用高级篇之一.
.推荐2本学J2ME的书~(新手必看).
.关于J2EE的几个观点.
.发送PDF文件.
.将Javaimage对象转换成PNG格式字.
.如何用IIS来实现OTA下载.
.使用Filter访问Session信息.
.利用Filter实现IP过滤.
.eclipse3.0开发j2me环境搭建.
.解决在J2ME平台上的手机游戏移植.
.J2ME程序开发初学者快速入门的九.
.快速创建Webapp.
.EnterpriseJavaBeansDistilled....
.入门:对J2EE初学者的学习流程介.
.在J2ME/MIDP中实现图像旋转(一).
.浅析J2EE应用中的时间值字段的数.
.BlueTooth探索系列(三)---发现.
.在J2EE上部署Web服务(Web Servi.
.基于MIDP1.0实现屏幕滚动.
.3D数学知识简介.

用RMS存储游戏积分

发表日期:2007-12-23


import Java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordFilter;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/**
 * A class used for storing and showing game scores.
 */
public class RMSGameScores extends MIDlet implements RecordFilter,
    RecordComparator {
  /*
   * The RecordStore used for storing the game scores.
   */
  private RecordStore recordStore = null;

  /*
   * The player name to use when filtering.
   */
  public static String playerNameFilter = null;

  /**
   * The constUCtor opens the underlying record store, creating it if
   * necessary.
   */
  public RMSGameScores() {
    // Create a new record store for this example
    try {
      recordStore = RecordStore.openRecordStore("scores", true);
    } catch (RecordStoreException rse) {
      System.out.println("Record Store Exception in the ctor." + rse);
      rse.printStackTrace();
    }
  }

  /**
   * startApp()
   */
  public void startApp() throws MIDletStateChangeException {
    RMSGameScores rmsgs = new RMSGameScores();
    rmsgs.addScore(100, "Alice");
    rmsgs.addScore(120, "Bill");
    rmsgs.addScore(80, "Candice");
    rmsgs.addScore(40, "Dean");
    rmsgs.addScore(200, "Ethel");
    rmsgs.addScore(110, "Farnsworth");
    rmsgs.addScore(220, "Alice");
    RMSGameScores.playerNameFilter = "Alice";
    System.out
        .println("Print all scores followed by Scores for Farnsworth");
    rmsgs.printScores();
  }

  /*
   * Part of the RecordFilter interface.
   */
  public boolean matches(byte[] candidate) throws IllegalArgumentException {
    // If no filter set, nothing can match it.
    if (this.playerNameFilter == null) {
      return false;
    }

    ByteArrayInputStream bais = new ByteArrayInputStream(candidate);
    DataInputStream inputStream = new DataInputStream(bais);
    String name = null;

    try {
      int score = inputStream.readInt();
      name = inputStream.readUTF();
    } catch (EOFException eofe) {   System.out.println(eofe);
      eofe.printStackTrace();
    } catch (IOException eofe) {
      System.out.println(eofe);
      eofe.printStackTrace();
    }
    return (this.playerNameFilter.equals(name));
  }

  /*
   * Part of the RecordComparator interface.
   */
  public int compare(byte[] rec1, byte[] rec2) {

    // Construct DataInputStreams for extracting the scores from
    // the records.
    ByteArrayInputStream bais1 = new ByteArrayInputStream(rec1);
    DataInputStream inputStream1 = new DataInputStream(bais1);
    ByteArrayInputStream bais2 = new ByteArrayInputStream(rec2);
    DataInputStream inputStream2 = new DataInputStream(bais2);
    int score1 = 0;
    int score2 = 0;
    try {
      // Extract the scores.
      score1 = inputStream1.readInt();
      score2 = inputStream2.readInt();
    } catch (EOFException eofe) {
      System.out.println(eofe);
      eofe.printStackTrace();
    } catch (IOException eofe) {
      System.out.println(eofe);
      eofe.printStackTrace();
    }

    // Sort by score
    if (score1 > score2) {
      return RecordComparator.FOLLOWS;
    } else if (score1 < score2) {
      return RecordComparator.PRECEDES;
    } else {
      return RecordComparator.EQUIVALENT;
    }
  }

  /**
   * Add a new score to the storage.
   * 
   * @param score
   *            the score to store.
   * @param playerName
   *            the name of the play achieving this score.
   */
  public void addScore(int score, String playerName) {
    // Each score is stored in a separate record, formatted with
    // the score, followed by the player name.
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream outputStream = new DataOutputStream(baos);
    try {
      // Push the score into a byte array.
      outputStream.writeInt(score);
      // Then push the player name.
      outputStream.writeUTF(playerName);
    } catch (IOException ioe) {
      System.out.println(ioe);
      ioe.printStackTrace();
    }

    // Extract the byte array
    byte[] b = baos.toByteArray();
    try {
      // Add it to the record store
      recordStore.addRecord(b, 0, b.length);
    } catch (RecordStoreException rse) {
      System.out.println(rse);
      rse.printStackTrace();
    }
  }

  /**
   * A helper method for the printScores methods.
   */
  private void printScoresHelper(RecordEnumeration re) {

    try {
      while (re.hasNextElement()) {
        int id = re.nextRecordId();
        ByteArrayInputStream bais = new ByteArrayInputStream(
            recordStore.getRecord(id));
        DataInputStream inputStream = new DataInputStream(bais);
        try {
          int score = inputStream.readInt();
          String playerName = inputStream.readUTF();
          System.out.println(playerName + " = " + score);
        } catch (EOFException eofe) {
          System.out.println(eofe);
          eofe.printStackTrace();
        }
      }
    } catch (RecordStoreException rse) {
      System.out.println(rse);
      rse.printStackTrace();
    } catch (IOException ioe) {
      System.out.println(ioe);
      ioe.printStackTrace();
    }
  }

  /**
   * This method prints all of the scores sorted by game score.
   */
  public void printScores() {
    try {
      // Enumerate the records using the comparator implemented
      // above to sort by game score.

      // No RecordFilter here. All records in the RecordStore
      RecordEnumeration re = recordStore.enumerateRecords(null, this,
          true);

      // Print all scores
      System.out.println("Print all scores...");
      printScoresHelper(re);

      // Enumerate records respecting a RecordFilter
      re = recordStore.enumerateRecords(this, this, true);

      //Print scores for Farnsworth
      System.out.println("Print scores for : " + this.playerNameFilter);
      printScoresHelper(re);
    } catch (RecordStoreException rse) {
      System.out.println(rse);
      rse.printStackTrace();
    }}

  /**
   * pauseApp()
   */
  public void pauseApp() {
    System.out.println("pauseApp()");
  }

  /**
   * destroyApp()
   * 
   * This closes our open RecordStore when we are destroyed.
   * 
   * @param cond
   *            true if this is an unconditional destroy false if it is not
   *            (ignored here and treated as unconditional)
   */
  public void destroyApp(boolean cond) {
    System.out.println("destroyApp( )");
    try {
      if (recordStore != null)
        recordStore.closeRecordStore();
    } catch (Exception ignore) {
      // ignore this
    }
  }

}

(出处:)


上一篇:调用 CGI 脚本 人气:621
下一篇:对RMS中的数据进行排序 人气:768
浏览全部J2EE/J2ME的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐