BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 07-16-2009, 03:29 AM   #1 (permalink)
Thumbs Must Hurt
 
Join Date: Jan 2009
Model: 7100T
PIN: N/A
Carrier: -
Posts: 73
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default updating proper position in a file

Please Login to Remove!

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?
Offline  
Old 07-16-2009, 06:37 AM   #2 (permalink)
Thumbs Must Hurt
 
Join Date: Jan 2009
Model: 7100T
PIN: N/A
Carrier: -
Posts: 73
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok, i've solved my problem. here is my code in case someone needs something similar:
Code:
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!
Offline  




Copyright 2004-2016 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.