哪里出错了~

badperson1 2009-11-17 09:16:25

为什么inputInteger1,inputInteger2输出的是4097、3840而不是15,16?

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.*;

/**
* @author stcLhong
*/
public class WriteReadExample extends MIDlet implements CommandListener{
private Display display;
private Alert alert;
private Form form;
private Command exit;
private Command start;
private RecordStore recordstore = null;

public WriteReadExample(){
display = Display.getDisplay(this);
exit = new Command("Exit",Command.EXIT,1);//sdf
start = new Command("Start",Command.SCREEN,1);
form = new Form("Record");
form.addCommand(exit);
form.addCommand(start);
form.setCommandListener(this);
}
public void startApp() {
display.setCurrent(form);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void commandAction(Command command,Displayable displayable){
if(command == exit){
destroyApp(true);
notifyDestroyed();
}
else if(command == start){
try{
recordstore = RecordStore.openRecordStore("myRecordStore", true);
}
catch(Exception error){
alert = new Alert("Error Creating",error.toString(),null,AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
try{
byte[] outputRecord;
String outputString = "First Record";
int outputInteger1 = 15;
int outputInteger2 = 16;
boolean outputBoolean = true;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DataOutputStream outputDataStream = new DataOutputStream(outputStream);
outputDataStream.writeUTF(outputString);
outputDataStream.writeInt(outputInteger1);
outputDataStream.writeInt(outputInteger2);
outputDataStream.writeBoolean(outputBoolean);
outputDataStream.flush();
outputRecord = outputStream.toByteArray();
recordstore.addRecord(outputRecord, 0, outputRecord.length);
outputStream.reset();
outputStream.close();
outputDataStream.close();
}
catch(Exception error){
alert = new Alert("Error Writing",error.toString(),null,AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
try{
String inputString = null;
int inputInteger1 = 0;
int inputInteger2 = 0;
boolean inputBoolean = false;
byte[] byteInputData = new byte[100];
ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
DataInputStream inputDataStream = new DataInputStream(inputStream);

for(int x = 1;x<=recordstore.getNumRecords();x++){
recordstore.getRecord(x,byteInputData,0);
inputString = inputDataStream.readUTF();
inputBoolean = inputDataStream.readBoolean();
inputInteger1 = inputDataStream.readInt();
inputInteger2 = inputDataStream.readInt();
inputStream.reset();
}
inputStream.close();
inputDataStream.close();
alert = new Alert("Reading",inputString+" "+inputInteger1+inputInteger2+" "+ inputBoolean,null,AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
catch(Exception error){
alert = new Alert("Error Reading",error.toString(),null,AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}

try{
recordstore.closeRecordStore();
}
catch(Exception error){
alert = new Alert("Error Closing",error.toString(),null,AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
if(RecordStore.listRecordStores()!=null){
try{
RecordStore.deleteRecordStore("myRecordStore");
}
catch(Exception error){
alert = new Alert("Error Removing",error.toString(),null,AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}
}
}
}
...全文
69 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
badperson1 2009-11-17
  • 打赏
  • 举报
回复
小弟拜服~呵呵,初学j2me,以后还望您老多指教指教~
kf156 2009-11-17
  • 打赏
  • 举报
回复
你贴链接,还玩递归啊...
kf156 2009-11-17
  • 打赏
  • 举报
回复
outputDataStream.writeUTF(outputString);
outputDataStream.writeInt(outputInteger1);
outputDataStream.writeInt(outputInteger2);
outputDataStream.writeBoolean(outputBoolean);

inputString = inputDataStream.readUTF();
inputBoolean = inputDataStream.readBoolean();
inputInteger1 = inputDataStream.readInt();
inputInteger2 = inputDataStream.readInt();

读的顺序和写的顺序不对。

不错不错,分数翻倍了
badperson1 2009-11-17
  • 打赏
  • 举报
回复
大哥,到这里看看http://topic.csdn.net/u/20091117/21/13c50097-eaa4-47b5-b232-7c6b259e4dd9.html这里我给了两倍的分数~我按照你说的弄了两个int,但是我输入时15,16为什么读的时候却是其他的数呢?

13,100

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧