不知为什么一直报这个错误:
java.io.EOFException
at java.io.DataInputStream.readFully(+48)
at java.io.DataInputStream.readUTF(+30)
at java.io.DataInputStream.readUTF(+4)
at gesanri.SetCanvas. <init>(+295)
at gesanri.MainCanvas.commandAction(+31)
做一个天气预报,可以保存要显示的城市名称
在增加城市页面,我的rms关键操作如下(其中SetCanvas.cityName是一个Vector,用来保存城市名,在执行此段代码之前,我已经把新增加的城市名添加进了这个Vector
try { recordstore = RecordStore.openRecordStore("cityStore", true); } catch (Exception e) { e.printStackTrace(); } try { byte[] outputRecord; String city = null; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); DataOutputStream outputDataStream = new DataOutputStream(outputStream); for(int i = 0; i < SetCanvas.cityName.size(); i++){ city = (String)SetCanvas.cityName.elementAt(i); outputDataStream.writeUTF(city); } outputDataStream.writeInt(SetCanvas.cityName.size()); outputDataStream.flush(); outputRecord = outputStream.toByteArray(); recordstore.setRecord(1,outputRecord, 0, outputRecord.length); outputStream.reset(); outputStream.close(); outputDataStream.close(); } catch (Exception e) { e.printStackTrace(); } try { recordstore.closeRecordStore(); } catch (Exception e) { e.printStackTrace(); }
SetCanvas里的rms关键操作如下,这里首先判断了如果是初次运行这个程序,则添加北京、上海、重庆3个城市
try { recordstore = RecordStore.openRecordStore("cityStore", true); if(recordstore.getNumRecords() == 0){ byte[] outputRecord; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); DataOutputStream outputDataStream = new DataOutputStream(outputStream); outputDataStream.writeUTF("北京"); outputDataStream.writeUTF("上海"); outputDataStream.writeUTF("重庆"); outputDataStream.writeInt(3); outputDataStream.flush(); outputRecord = outputStream.toByteArray(); recordstore.addRecord(outputRecord, 0, outputRecord.length); outputStream.reset(); outputStream.close(); outputDataStream.close(); cityName.addElement("北京"); cityName.addElement("上海"); cityName.addElement("重庆"); } else{ cityName.removeAllElements(); String inputString = null; int inputInteger = 0; byte[] byteInputData = new byte[100]; ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData); DataInputStream inputDataStream = new DataInputStream(inputStream); recordstore.getRecord(1,byteInputData,0); inputInteger = inputDataStream.readInt(); for(int i = 0; i < inputInteger; i++){ inputString = inputDataStream.readUTF(); cityName.addElement(inputString); } inputStream.reset(); inputStream.close(); inputDataStream.close(); } }catch (Exception e) { e.printStackTrace(); } try { recordstore.closeRecordStore(); } catch (Exception e) { e.printStackTrace(); }
------解决方法--------------------------------------------------------
//write ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); DataOutputStream outputDataStream = new DataOutputStream(outputStream); outputDataStream.writeInt(SetCanvas.cityName.size());//先写size for(int i = 0; i < SetCanvas.cityName.size(); i++){ city = (String)SetCanvas.cityName.elementAt(i); outputDataStream.writeUTF(city); } outputRecord = outputStream.toByteArray(); recordstore.setRecord(1,outputRecord, 0, outputRecord.length); outputDataStream.close();//readByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData); DataInputStream inputDataStream = new DataInputStream(inputStream); recordstore.getRecord(1,byteInputData,0); inputInteger = inputDataStream.readInt(); for(int i = 0; i < inputInteger; i++){ inputString = inputDataStream.readUTF(); cityName.addElement(inputString); } inputDataStream.close();