📄 carstore.java
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.microedition.rms.*;
import java.io.*;
public class CarStore
{
// These two static final ints will
// be used to filter the records via
// a RecordFilter implementation.
public static final int CASH_RECORD = 1;
public static final int WEAPON_RECORD = 2;
// The player's current weapons
private Weapon[] weapons;
// The player's current cash
private int cash = 0;
// Build a new CarStore and set the
// members
CarStore()
{
}
// Writes the cash and weapon data to a new
// RecordStore.
public void writeToStore()
{
try
{
// Let's make sure we're dealing with a fresh new RecordStore
// (since we're still relying a little on recordId ordering)
// and remove it if it's still in device storage.
RecordStore.deleteRecordStore("Car");
}
catch (RecordStoreException rse)
{
// This probably means we haven't created a store yet.
System.err.println(rse);
}
RecordStore carDB = null;
// Now we're ready
try
{
// Now we'll create a new one.
carDB = RecordStore.openRecordStore( "Car", true );
// Build some reusable data handling objects.
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
byte[] data = null;
// First, we're going to write the player's
// current cash to the car's RecordStore
// We'll id the record as a cash record...
dout.writeByte(CASH_RECORD);
// and then write the cash amount
dout.writeInt(cash);
// and now bundle the Stream into a byte
// array and write a new record. It's
// the very first record in the RecordStore,
// so we know it'll have the recordId of 1.
data = bout.toByteArray();
// Add the array to the RecordStore
carDB.addRecord(data, 0, data.length );
// and reset the Streams to
// properly handle the Weapons.
bout.reset();
// Iterate through the car's Weapons array
for (int i = 0; i < weapons.length; i++)
{
// Write the weapon record tag..
dout.writeByte(WEAPON_RECORD);
// Write the weapon type...
dout.writeInt(weapons[i].getWeaponType());
// Write the weapon's ammo
dout.writeInt(weapons[i].getWeaponAmmo());
// Write the Weapon's time value
dout.writeInt(weapons[i].getWeaponTime());
// Write the weapon's name
dout.writeUTF(weapons[i].getName());
// Write the weapon's description
dout.writeUTF(weapons[i].getDescription());
dout.flush();
// Pack the Byte Stream (aka the Data Stream)
// into the data byte array...
data = bout.toByteArray();
// Add the array to the RecordStore
carDB.addRecord(data, 0, data.length );
// and reset the Streams to
// properly handle the next Weapon.
bout.reset();
}
carDB.closeRecordStore();
dout.close();
bout.close();
}
catch (Exception e)
{
try { carDB.closeRecordStore(); } catch (Exception e2) { }
e.printStackTrace();
}
}
public boolean readStore()
{
RecordStore carDB = null;
try
{
// Open the RecordStore built above...
carDB = RecordStore.openRecordStore( "Car", false );
// Build some reusable data handling objects.
ByteArrayInputStream bin = null;
DataInputStream din = null;
byte[] data = null;
// First, we'll get the cash record out and set
// the cash value. Remember that this record
// was added before any other so we know it's
// at recordId == 1.
data = carDB.getRecord(1);
// Set up the streams to read back the
// stored variables
bin = new ByteArrayInputStream(data);
din = new DataInputStream(bin);
// discard the CASH_RECORD byte
din.readByte();
// and now grab the actual cash value stored.
cash = din.readInt();
System.out.println("Cash: "+cash);
// clean up for the weapons handling
din.close();
bin.close();
// Prepare the Weapons array for the new data
// (We build it to (getNumRecords()-1) to account
// for the cash record.)
weapons = new Weapon[carDB.getNumRecords()-1];
// The following sets up a RecordEnumeration
// object with our CarItemFilter RecordFilter
// and a null RecordComparator which means
// it will simply iterate the weapon records
// in no particular order. We're assuming, here,
// that the Weapon records found won't need to be
// in a certain sequence but can be rebuilt
// willy-nilly. You can always build your
// own RecordComparator to put the enumeration into
// some specific order.
CarItemFilter filter = new CarItemFilter(WEAPON_RECORD);
RecordEnumeration re = carDB.enumerateRecords(filter, null, false);
// Prepare the necessary temp variables and
int count = 0;
int type = 0;
int time = 0;
int ammo = 0;
String name = "";
String description = "";
while (re.hasNextElement())
{
// This next call returns the record's byte array and
// advances the RecordEnumerator's pointer to the record
// beyond the one returned.
data = re.nextRecord();
// Set up the streams...
bin = new ByteArrayInputStream(data);
din = new DataInputStream(bin);
// Read and discard the weapon record id
din.readByte();
// Read the weapon type...
type = din.readInt();
// Read the weapon's ammo
ammo = din.readInt();
// Read the Weapon's time value
time = din.readInt();
// Read the Weapon's name
name = din.readUTF();
System.out.println(count+") Got the Weapon name: "+name);
// Read the Weapon's description
description = din.readUTF();
// Now rebuild the weapon into the
// weapons array.
weapons[count++] = new Weapon(type, time, ammo,name,description);
// Clean up
din.close();
bin.close();
}
}
catch (Exception e)
{
System.err.println("Exception in readStore()"+e);
// This probably means we haven't created a store yet.
try { carDB.closeRecordStore(); } catch (Exception e2) { }
return false;
}
try { carDB.closeRecordStore(); } catch (Exception e2) { }
System.out.println("Done Reading From Store");
return true;
}
public int getCash()
{
return cash;
}
public void setCash(int amount)
{
cash = amount;
}
public Weapon[] getWeapons()
{
return weapons;
}
public void setWeapons(Weapon[] w)
{
weapons = w;
}
}
// This class is the RecordFilter we'll use to
// find all the Weapon records.
class CarItemFilter implements RecordFilter
{
// This will contain the car item id
// we'll be looking for in the records.
private byte filterValue;
// Builds a new CarItemFilter for the
// specific car record id we want to
// find
public CarItemFilter(int filterInt)
{
filterValue = (byte)filterInt;
}
// This is the RecordFilter method used
// by the RecordEnumeration to determine
// if the record is a "match" and needs
// to be included in its enumeration.
public boolean matches(byte[] candidate)
{
if (candidate == null || candidate.length == 0)
{
return false;
}
// Since, in the CarStore.writeToStore() method,
// we wrote the item type byte to the records
// before anything else, the byte at index 0
// should be our id to filter.
return (candidate[0] == filterValue);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -