Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000
Accepted Solution

updating proper positions in file

hi,

i would like to know Your suggestion on how can i solve such a task:

 

i've got a file to each 7 parameters can be wrote. each parameter is separate using ";". so i've got:

 

A;B;C;D;E;F;G

 

first six paramters are integers which can be incremented. i must write a function which will allow me add another number to the proper parameter. So, if i want add 4 to the 3rd parameter i must do an update on the third parameter: C+4 and write it back to the file, so that i'll get:

A;B;C+4;D;E;F;G

 

my problem is that i cannot solve setting the position. ok, par example-i use DataBuffer, i can 'setPostion' and write from that, but.. what if my number will get bigger? firstly it  takes one position (par example : 1), but after few updates it'll get bigger and takes three positions (par example: 120). the parameter can overwrite part of another parameter.

 

can anyone suggest me how can i solve it? i hope i've described clearly what my problem is..

 

i'll be really gratefull for any ideas!

 

     kind regards

 

Developer
Developer
Posts: 283
Registered: ‎07-22-2008
My Device: Not Specified

Re: updating proper positions in file

How about reading the parameters into a buffer in memory, update what needs to be updated and overwrite the whole file. Even better, is to keep the parameters in memory at all times, and whenever they are updated, you write them to the file. We do this all the time. In your case, these parameters seems to be small ( in terms of memory space), so it should not be a problem.

 

Rab

Developer
Developer
Posts: 1,123
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook
My Carrier: Verizon

Re: updating proper positions in file

you can do as rabb suggested or read the data in as a string, search for the ';' as a seperator and the convert the values to integers update and rewrite them out.
Developer
Posts: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000

Re: updating proper positions in file

rab, i'm trying to implement it like You've suggested, with reading to a buffer and then update proper position, but something is not working:

 

public void addToLog(int _where, FileConnection _fc, int _data, String _dataToday){ try{ _dos = _fc.openOutputStream(); _dis = _fc.openDataInputStream(); _stringBuffer = new StringBuffer(); int _c; while((_c = _dis.read()) != -1){ _stringBuffer.append(_c); }//end of while _db = new DataBuffer(); _db.writeChars(_stringBuffer.toString()); //update _db.setPosition(_where); _db.writeChars(_data+";"); _dos.write(_db.getArray(), 0, _db.getArray().length); _dos.flush(); _dos.close(); _dis.close(); } catch(Exception e){} }//end of addToLog() method

 

 

 

when i call the method for the first time, par example:

addToLog(2, _fileConnection, 2, "");

 

and what i get in the file is: 2;

but.. first of all-it is not on the 2nd position but in the beginning of the line. second of all-when i try co call the method for the second time, par example:

addToLog(0, _fileCOnnection, 8, "") - i'm expecting to get '8;' on the 0th position, but what i get is total trash:

8;005900000000000000000000

 

what is that? what is wrong with this method?

Highlighted
Developer
Posts: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000

Re: updating proper positions in file

ok, i've solved my problem. here is my code in case someone needs something similar:

 

public void addToLog(int _where, FileConnection _fc, int _data, String _dataToday){ try{ _dis = _fc.openDataInputStream(); _stringBuffer = new StringBuffer(); int _c; while( (_c = _dis.read()) != -1 ){ _stringBuffer.append((char)_c); } _dis.close(); String _string = _stringBuffer.toString(); String _search = ";"; String _result = ""; int _i; int _j = 0; String[] _arr = new String[7]; do{ _i = _string.indexOf(_search); if( _i != -1 ){ _result = _string.substring(0, _i); _arr[_j] = _result; _j++; _result = _string.substring(_i + _search.length()); _string = _result; } }while( _i != -1); //update _arr[_what] = String.valueOf(_data); _stringBuffer = new StringBuffer(); //convert to string, add ";" between every item for(int _a = 0; _a < _arr.length; _a++){ _stringBuffer.append(_arr[_a]); _stringBuffer.append(";"); } _dos = _fc.openOutputStream(); _db = new DataBuffer(); _db.writeChars(_stringBuffer.toString()); _dos.write(_db.getArray(), 0, _db.getArray().length); _dos.flush(); _dos.close(); }catch(Exception e){ } }//end of addToLog() method

 

 

hope it will be usefull for someone.

 

 kind regards!