I always get confused and get headache whenever I read about stream.
In the following program, I understand how to write data to the recordstore.
The DataOutputStream class has methods that write specific data types to a buffer.
Three of these methods are used in this example. These are writeUTF() method,
writeBoolean() method, and writeInt() method. Each is passed the appropriate data.
The buffered data is placed in the data stream (ByteArrayOutputStream ???) by calling the flush() method. The
stream is converted to a byte array by calling the toByteArray() method, which returns
a reference to the byte array of the stream. This reference is passed to the addRecord()
method which saves the byte array as a new
record in the record store. The ByteArrayOutputStream object's internal store is cleared
by calling the reset() method.
But I know nothing how to read data from a recordstore with ByteArrayInputStream and DataInputStream and how they work in detail.
We have to write like that.
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DataOutputStream outputDataStream =new DataOutputStream(outputStream);
Why we need to pass the referende to the byte array output stream in the DataOutPutStream() constructor?
Similarly,
ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
DataInputStream inputDataStream =new DataInputStream(inputStream);
I try to find some explanation from google but I don't understand.
Please point out some book or link if there is any thing u want to point out.
But please kindly also explain me with your own words.
Thank you very much.
Code:try { byte[] outputRecord; String outputString = "First Record"; int outputInteger = 15; boolean outputBoolean = true; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); DataOutputStream outputDataStream = new DataOutputStream(outputStream); outputDataStream.writeUTF(outputString); outputDataStream.writeBoolean(outputBoolean); outputDataStream.writeInt(outputInteger); 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 inputInteger = 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(); inputInteger = inputDataStream.readInt(); inputStream.reset(); }

Reply With Quote


