Java me RMS在标枪教学中的应用
我正在尝试用RMS在Jave ME中编写应用程序。应用程序存储有关courier客户的信息Java me RMS在标枪教学中的应用,java-me,rms,Java Me,Rms,我正在尝试用RMS在Jave ME中编写应用程序。应用程序存储有关courier客户的信息 import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.rms.*; import java.io.ByteArrayOutputStream; import java.io.ByteArrayInputStream; import java.io.DataOutput
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.DataOutputStream;
import java.io.DataInputStream;
public class ClientsApp extends MIDlet implements CommandListener {
// controls
private Display screen = null;
private List menu = null;
private Form addClientForm = null;
private Form showClientForm = null;
private RecordStore clients;
private ByteArrayOutputStream stream = null;
private DataOutputStream out = null;
private byte[] dates;
TextField name = null;
TextField surname = null;
TextField email = null;
TextField phone = null;
DateField date = null;
TextField price = null;
TextField description = null;
// comands
private final Command backCommand;
private final Command mainMenuCommand;
private final Command exitCommand;
private final Command addClientCommand;
public ClientsApp() {
// initializating controls and comands
menu = new List("Lista klientów", Choice.IMPLICIT);
backCommand = new Command("Cofnij", Command.BACK, 0);
mainMenuCommand = new Command("Main", Command.SCREEN, 1);
exitCommand = new Command("Koniec", Command.EXIT, 2);
addClientCommand = new Command("Zapisz", Command.OK, 3);
stream = new ByteArrayOutputStream();
out = new DataOutputStream(stream);
menu.append("Dodaj klienta", null);
menu.append("Przegladaj klientow", null);
menu.append("Usun klienta", null);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
screen = Display.getDisplay(this);
screen.setCurrent(menu);
try {
clients = RecordStore.openRecordStore("clients", false, RecordStore.AUTHMODE_PRIVATE, false);
}
catch(RecordStoreException exc) {
}
menu.addCommand(exitCommand);
menu.setCommandListener(this);
}
public void commandAction(Command cmd, Displayable dsp) {
if(cmd.getCommandType() == Command.EXIT) {
try{
destroyApp(false);
notifyDestroyed();
}
catch(Exception exc) {
exc.printStackTrace();
}
}
else if(cmd.getCommandType() == Command.BACK) {
screen.setCurrent(menu);
}
else if(cmd.getCommandType() == Command.OK) {
try {
out.writeUTF(name.getString());
out.writeUTF(surname.getString());
out.writeUTF(email.getString());
out.writeUTF(phone.getString());
out.writeUTF(date.getDate().toString());
out.writeUTF(price.getString());
out.writeUTF(description.getString());
dates = stream.toByteArray();
clients.addRecord(dates, 0, dates.length);
stream.close();
out.close();
clients.closeRecordStore();
}
catch(Exception exc) {
}
}
else {
List option = (List) screen.getCurrent();
switch(option.getSelectedIndex()) {
case 0 : {
addClients();
break;
}
case 1 : {
showClients();
break;
}
case 2 : {
deleteClients();
break;
}
}
}
}
protected void addClients() {
addClientForm = new Form("Dodaj klienta");
name = new TextField("Imię klienta", "", 10, TextField.ANY);
surname = new TextField("Nazwisko klienta", "", 15, TextField.ANY);
email = new TextField("Email klienta", "", 20, TextField.EMAILADDR);
phone = new TextField("Numer telefonu", "", 12, TextField.PHONENUMBER);
date = new DateField("Data dostarczenia", DateField.DATE);
price = new TextField("Do zapłaty", "", 6, TextField.NUMERIC);
description = new TextField("Uwagi", "", 50, TextField.ANY);
addClientForm.append(name);
addClientForm.append(surname);
addClientForm.append(email);
addClientForm.append(phone);
addClientForm.append(date);
addClientForm.append(price);
addClientForm.append(description);
screen.setCurrent(addClientForm);
addClientForm.addCommand(backCommand);
addClientForm.addCommand(addClientCommand);
addClientForm.setCommandListener(this);
}
protected void showClients() {
TextBox info = new TextBox("Klienci", null, 100, 0);
RecordEnumeration iterator = null;
String str = null;
byte[] temp = null;
try {
iterator = clients.enumerateRecords(null, null, false);
while(iterator.hasNextElement()) {
temp = iterator.nextRecord();
}
for(int i = 0; i < temp.length; i++) {
str += (char) temp[i];
}
System.out.println(str);
clients.closeRecordStore();
}
catch(Exception exc) {
}
info.setString(str);
screen.setCurrent(info);
}
}
import javax.microedition.midlet.*;
导入javax.microedition.lcdui.*;
导入javax.microedition.rms.*;
导入java.io.ByteArrayOutputStream;
导入java.io.ByteArrayInputStream;
导入java.io.DataOutputStream;
导入java.io.DataInputStream;
公共类ClientsApp扩展MIDlet实现CommandListener{
//控制
专用显示屏=空;
私有列表菜单=null;
私有表单addClientForm=null;
私有表单showClientForm=null;
私人唱片店客户;
private ByteArrayOutputStream=null;
私有DataOutputStream out=null;
专用字节[]日期;
TextField name=null;
TextField姓氏=null;
TextField email=null;
TextField phone=null;
DateField date=null;
TextField price=null;
TextField description=null;
//科曼
私有最终命令backCommand;
专用最终命令mainMenuCommand;
私有最终命令exitCommand;
私有最终命令addClientCommand;
公共客户端{
//初始化控件和命令
菜单=新列表(“Lista klientów”,Choice.IMPLICIT);
backCommand=新命令(“Cofnij”,Command.BACK,0);
mainMenuCommand=新命令(“Main”,Command.SCREEN,1);
exitCommand=新命令(“Koniec”,Command.EXIT,2);
addClientCommand=新命令(“Zapisz”,Command.OK,3);
stream=newbytearrayoutputstream();
out=新数据输出流(流);
menu.append(“Dodaj-klienta”,null);
menu.append(“Przegladaj klientow”,null);
menu.append(“通常为klienta”,null);
}
受保护的void destroyApp(布尔arg0)引发MIDletStateChangeException{
}
受保护的无效pauseApp(){
}
受保护的void startApp()引发MIDletStateChangeException{
screen=Display.getDisplay(这个);
屏幕设置电流(菜单);
试一试{
clients=RecordStore.openRecordStore(“clients”,false,RecordStore.AUTHMODE\u PRIVATE,false);
}
捕获(RecordStoreException exc){
}
menu.addCommand(exitCommand);
menu.setCommandListener(这个);
}
公共无效命令操作(命令cmd,可显示dsp){
if(cmd.getCommandType()==Command.EXIT){
试一试{
应用程序(假);
通知销毁();
}
捕获(异常exc){
exc.printStackTrace();
}
}
else if(cmd.getCommandType()==Command.BACK){
屏幕设置电流(菜单);
}
else if(cmd.getCommandType()==Command.OK){
试一试{
out.writeUTF(name.getString());
out.writeUTF(姓氏.getString());
out.writeUTF(email.getString());
out.writeUTF(phone.getString());
out.writeUTF(date.getDate().toString());
out.writeUTF(price.getString());
out.writeUTF(description.getString());
日期=stream.toByteArray();
clients.addRecord(日期,0,日期,长度);
stream.close();
out.close();
clients.closeRecordStore();
}
捕获(异常exc){
}
}
否则{
列表选项=(列表)screen.getCurrent();
开关(option.getSelectedIndex()){
案例0:{
addClients();
打破
}
案例1:{
showClients();
打破
}
案例2:{
删除客户端();
打破
}
}
}
}
受保护的void addClients(){
addClientForm=新表格(“Dodaj klienta”);
name=新文本字段(“Imięklienta”,“”,10,TextField.ANY);
姓氏=新文本字段(“Nazwisko klienta”,15,TextField.ANY);
email=newtextfield(“email klienta”,20,TextField.EMAILADDR);
电话=新的文本字段(“电话号码”,12,TextField.PHONENUMBER);
日期=新的日期字段(“数据dostarczenia”,DateField.date);
价格=新文本字段(“Do zapłaty”,6,TextField.NUMERIC);
description=新文本字段(“Uwagi”,50,TextField.ANY);
addClientForm.append(名称);
addClientForm.append(姓氏);
addClientForm.append(电子邮件);
addClientForm.append(电话);
addClientForm.append(日期);
addClientForm.append(价格);
addClientForm.append(说明);
screen.setCurrent(addClientForm);
addClientForm.addCommand(backCommand);
addClientForm.addCommand(addClientCommand);
addClientForm.setCommandListener(此);
}
受保护的void showClients(){
TextBox info=新的文本框(“Klienci”,null,100,0);
RecordEnumeration迭代器=null;
字符串str=null;
字节[]温度=null;
试一试{
迭代器=clients.enumerateRecords(null、null、false);
while(iterator.hasNextElement()){
temp=iterator.nextRecord();
}
对于(int i=0;i
从RecordStore写入/读取信息无效。我没有任何异常抛出。谁能帮帮我吗
对不起,我的语言不好 您确定没有任何异常吗?捕获块是空的。。。 我看到几个问题: 您不应该打开CreateIfEssential(第二个参数)设置为true的记录存储吗? 在ShowClients方法中,您应该使用DataInputStream从记录(字节数组“temp”)中读取项,循环温度是奇怪的。当存储区为空时,检查null'temp'以避免NPE也丢失了。 在OK命令下,以及在ShowClient中,存储区都已关闭,因此下次它将因RecordStoreNotOpenException而失败
在调用流之前,我也会考虑FLASHOUT,ToBytEraye(),虽然在这种情况下(DATAOUTPUSTRATA/BYTAREAROUTPUT流),它不过是一个好的实践。