BlackBerry Forums Support Community
              

Closed Thread
 
Thread Tools
Old 07-16-2009, 03:29 AM   #1
misty83
Thumbs Must Hurt
 
Join Date: Jan 2009
Model: 7100T
PIN: N/A
Carrier: -
Posts: 73
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
misty83
Thumbs Must Hurt
 
Join Date: Jan 2009
Model: 7100T
PIN: N/A
Carrier: -
Posts: 73
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  
Closed Thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


RCA 2N3054 BIPOLAR SINGLE TRANSISTOR NPN, 55 V, 3 MHz, 4A, 25W  165 PC LOT New picture

RCA 2N3054 BIPOLAR SINGLE TRANSISTOR NPN, 55 V, 3 MHz, 4A, 25W 165 PC LOT New

$495.00



MOSFET - IRFZ44N 55V - Transistor  for Arduino Pi  TTL picture

MOSFET - IRFZ44N 55V - Transistor for Arduino Pi TTL

$54.79



MRF1K50N Wideband RF Power LDMOS Transistor 1500W CW over 1.8-500 MHz picture

MRF1K50N Wideband RF Power LDMOS Transistor 1500W CW over 1.8-500 MHz

$135.00



Portable 600pc 15Value NPN PNP Transistor TO-92 Assortment Kit Set /Box Hot picture

Portable 600pc 15Value NPN PNP Transistor TO-92 Assortment Kit Set /Box Hot

$12.66



MOSFET - IRF640N 200V 18A - Transistor for Arduino Pi TT picture

MOSFET - IRF640N 200V 18A - Transistor for Arduino Pi TT

$45.64



2SA733-Q Original New NEC TO-92 Transistor picture

2SA733-Q Original New NEC TO-92 Transistor

$10.95







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