Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Oct 2010

    Cannot write int (integer) values to file using write(int i) method

    Hello all, I couldn't find my issue either on the internet or here so here I am writing.

    I am transferring an integer array into a file. I can't write integer values using outputstream.write (int i) method. Converting the integer variable into bytes fails as well. However, converting anything to string and then to bytes (using string.getBytes() ) and then outputstream.write(byte [] buffer) works. I may attach my code if needed.
    At first I thought it's somehow related to the fact I passed arguments like
    [I]for (int i = 0; i < 1000; i++){
    for (int j = 0; j < 4; j++){
    write (array[j]);
    } }

    but tried with a temporal variable and it still didn't work. I made clean build as well, to no effect.

    I am opening the stream as follows
    fconn = (FileConnection)Connector.open(url, Connector.READ_WRITE);
    if (!fconn.exists())
    OutputStream ops = fconn.openOutputStream(fconn.fileSize());

    I am using Netbeans 7.0 and Symbian Belle SDK and run the program on an E52.

    I'd be very grateful for your help
    Last edited by antonen; 2012-02-28 at 21:23.

  2. #2
    Nokia Developer Expert
    Join Date
    Aug 2011

    Re: Cannot write int (integer) values to file using write(int i) method

    Hi antonen,

    The reason why you cannot use the method write(int b) to export an integer to the output stream is because, what really happens is that you write raw binary data to the stream. What is written on the stream is not the integer value, but instead the 8 lower bits of this integer, i.e. a single byte. The higher 24 bits (or 3 bytes) of the integer b are just discarded by the method.

    If you use this method, you have to handle the translation of your integer value to raw binary sequences that can be properly displayed by the charset used by the system.
    If for example, you need to store the integer value 10 in Unicode, based on this table here, you would need to write two bytes to the stream:

    outputstream.write(49);  //this writes the integer 1, in HEX 31
    outputstream.write(48);  //this writes the integer 0, in HEX 30
    Doing this translation manually makes things a bit more complicated. That's why, you are better off using a String and letting the getBytes method do the translation for you.
    Last edited by skalogir; 2012-02-29 at 00:18.

Similar Threads

  1. Replies: 2
    Last Post: 2011-06-30, 11:35
  2. very simple middlet class to write to write a text file
    By a_sina_a in forum Mobile Java General
    Replies: 10
    Last Post: 2008-03-07, 16:35
  3. How can I write binary values using RFile?
    By mabdulmoniem in forum Symbian
    Replies: 5
    Last Post: 2007-10-09, 14:12
  4. read integer values out of a text file
    By michael.schrenk in forum Symbian
    Replies: 4
    Last Post: 2005-02-19, 19:55
  5. How to read integer and float values from a file?
    By Falcons in forum Symbian Tools & SDKs
    Replies: 2
    Last Post: 2003-06-04, 04:57

Posting Permissions

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