×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    1

    Red face newbie (sorry!): textbox contents to byte array

    Sorry, i know this is so basic but struggling with it!

    I want to take the contents of a textbox from my midlet app and put it in to a byte array so I can use the setRecord of the RecordStore object to save it.

    Would REALLY appreciate any help!

    dan

  2. #2
    Registered User
    Join Date
    May 2005
    Location
    Cape Town, SA
    Posts
    11
    Hi, had a hard time myself getting this. Here's what I did:

    RecordStore file = null;
    byte[] data;
    String newText = getString();
    int numRecords = 0;
    if(fileToOpen!=null)
    {
    file = RecordStore.openRecordStore(fileToOpen, true, 1, true);
    numRecords = file.getNumRecords();
    file.setMode(1, true);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(bos);
    if(newText=="") newText = "Nothing to save!";
    out.writeUTF(newText);
    data = bos.toByteArray();
    out.close();
    file.addRecord(data, 0, data.length);
    file.closeRecordStore();
    }

    You have to add your own try ... catch statements.

    Reading has similar functions, i.e.
    ByteArrayInputStream instead of ByteArrayOutputStream

    Just go through all the examples there are, you'll find whatever you need.

  3. #3
    Regular Contributor
    Join Date
    Sep 2004
    Location
    Barcelona, Spain
    Posts
    68
    //Well, to get the string:
    String s=textbox.getString();

    //To convert it to bytearray:
    byte[] b=s.getBytes();

    //To write the register:
    try{
    //first open the recordstore:
    recStore = RecordStore.openRecordStore(BD, true );

    //Then write the record:
    recStore.addRecord(b, 0,b.length);

    //And finally close:
    recStore.closeRecordStore();
    }catch(Exception ex){}



    Hope it helps,
    Nuria.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×