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
i've tried to implement it like this-read data, store it into DataBuffer, update on a proper position and then-write again:
Code:
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? or maybe You have some other ideas?