İçindekilerGirişİndex
YukarıİlkÖncekiSonrakiSon
Geriİleri
Yazdır
Zafer Teker
tekzaf@yahoo.com

Random Üretilmiş Bir Sayıyı Kaydetme ve Daha Önce Kaydedilmiş Bir Sayıyı Okuyan Midlet

Giriş

Bu örnekteki midlet eğer daha önce kaydedilmiş bir sayı varsa bu sayıyı okuyup ekranda göstermektedir. Eğer daha önce bir sayı kaydedilmemişse yeni bir sayı üretip kaydetmektedir. Bu işlem TestCanvas class'ının kurucusunda yapılmaktadır.

Göster Gizle Kopar Satır Gizle Satır Göster
  1 import javax.microedition.midlet.*;
  2 import javax.microedition.lcdui.*;
  3 import java.io.*;
  4 import java.util.*;
  5 import javax.microedition.rms.*;
  6 public class RecordTestMidlet extends MIDlet{
  7   private final static Random random=new Random();
  8   public void startApp(){
  9     TestCanvas canvas=new TestCanvas(this);
 10     Display.getDisplay(this).setCurrent(canvas);
 11   }
 12   public void pauseApp(){}
 13   public void destroyApp(boolean b){}
 14   public void exit(){
 15     destroyApp(false);
 16     notifyDestroyed();
 17   }
 18   class TestCanvas extends Canvas implements CommandListener{
 19     private RecordTestMidlet middlet;
 20     private Command exitCommand=new Command("Exit",Command.EXIT,1);
 21     private int randomNumber;
 22     private String recordName="Test";
 23     private int recordID=1;
 24     private RecordStore testRecordStore;
 25     public TestCanvas(RecordTestMidlet middlet){
 26       this.middlet=middlet;
 27       addCommand(exitCommand);
 28       setCommandListener(this);
 29       try{
 30         String[] records=RecordStore.listRecordStores();
 31         if(records!=null){
 32           if(hasTestRecord(records)){
 33             randomNumber=getRandomNumberOfRecordStore();
 34           }
 35         }else{
 36           randomNumber=random.nextInt();
 37           storeRandomNumberToRecordStore(randomNumber);
 38         }
 39       }catch(Exception e){
 40         e.printStackTrace();
 41       }
 42     }
 43     private int getRandomNumberOfRecordStore() throws Exception{
 44       testRecordStore=RecordStore.openRecordStore(recordName,false);
 45       byte data[]=testRecordStore.getRecord(recordID);
 46       ByteArrayInputStream bais=new ByteArrayInputStream(data);
 47       DataInputStream dis=new DataInputStream(bais);
 48       int number=dis.readInt();
 49       dis.close();
 50       bais.close();
 51       return number;
 52     }
 53     private void storeRandomNumberToRecordStore(int number) throws Exception{
 54       testRecordStore=RecordStore.openRecordStore(recordName,true);
 55       ByteArrayOutputStream baos=new ByteArrayOutputStream();
 56       DataOutputStream dos=new DataOutputStream(baos);
 57       dos.writeInt(number);
 58       byte data[]=baos.toByteArray();
 59       dos.close();
 60       baos.close();
 61       testRecordStore.addRecord(data,0,data.length);
 62     }
 63     private boolean hasTestRecord(String[] records){
 64       for(int i=0;i<records.length;i++){
 65         if(records[i].equals(recordName)){
 66           return true;
 67         }
 68       }
 69       return false;
 70     }
 71     public void commandAction(Command c, Displayable d){
 72      if(c==exitCommand){
 73        middlet.exit();
 74      }
 75     }
 76     public void paint(Graphics g){
 77       clear(g);
 78       String numberS=Integer.toString(randomNumber);
 79       g.drawString(numberS,10,10,Graphics.TOP|Graphics.LEFT);
 80     }
 81     private void clear(Graphics g){
 82       g.setColor(255,255,255);
 83       g.fillRect(0,0,getWidth(),getHeight());
 84       g.setColor(200,200,100);
 85     }
 86   }
 87 }

RecordStore javax.microedition.rms paketindeki tek class'dır. Bu class'ta üç static method bulunmaktadır.

Önce tüm kayıtların listesini alıyoruz. Eğer kayıtlar null ise veya Test adında bir kayıt yoksa yeni random yaratıyoruz ve bu sayıyıkaydediyoruz. Eğer böyle bir kayıt varsa bu kayıttan kaydedilmiş sayıyı alıp randomNumber sayısını set edeiyoruz. getRandomNumberOfRecordStore methodu kadedilmiş sayıyı bulmak işini yapmaktadır. Önce

testRecordStore=RecordStore.openRecordStore(recordName,false);

ile RecordStore açılmaktadır. Her recordStore kayıtlar bir id'i ile eklenir ve okunur. İlk eklenen kayıtın id'si 1 olarak verilir.

ByteArrayInputStream bais=new ByteArrayInputStream(data);
DataInputStream dis=new DataInputStream(bais);
int number=dis.readInt();

ile daha önce kaydedilmiş int değeri okunur.

storeRandomNumberToRecordStore ise verilen bir sayıyı kaydetme işini yapmaktadır.

testRecordStore=RecordStore.openRecordStore(recordName,true);

ile RecordStore açılmaktadır.

ByteArrayOutputStream baos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(baos);
dos.writeInt(number);

ile DataOutputStream'e number yazılır. Bu data daha sonra

byte data[]=baos.toByteArray();

ile byte dizisine çevrilir ve

testRecordStore.addRecord(data,0,data.length);

ile bu byte dizisi kayıt edilir

Dosya Listesi

İçindekilerGirişİndex
YukarıİlkÖncekiSonrakiSon
Geriİleri
Yazdır