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
Highlighted
Developer
Posts: 61
Registered: ‎09-06-2010
My Device: Not Specified

How to read and write data in blackberry

Hi,can any one provide sample application on reading and writing the data using the interfaces in the java.io package.I searched in google but did not find any thing satisfied.

Developer
Posts: 324
Registered: ‎03-10-2010
My Device: Bold 9780, DevAlpha
My Carrier: Vodafone CZ

Re: How to read and write data in blackberry

import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import net.rim.device.api.io.IOUtilities;


public class mStringOperations
{
	public static String getTextFile(String fName)
	{
		 String sOut = null;
		 FileConnection fconn = null;
		 InputStream is = null; // NOT DataInputStream
		 try {
		   fconn = (FileConnection) Connector.open(fName, Connector.READ);
		   if (fconn != null)
		   {
			   is = fconn.openDataInputStream();
			   if (is!=null)
			   {
				   sOut = getResourceFile(is);
			   }
		   }
		 }catch (Exception e)
		 {
			 
		 }
		 
		 try
		 {
			 if (is != null)
			 {
				 is.close();
			 }
			 if (fconn != null)
			 {
				 fconn.close();
			 }
			 
		 }catch (Exception e)
		 {
		 }
		return sOut;
	}
	
	public static String getResourceFile(InputStream is)
	{
		String sOut = null;
		try
		{
			byte data[] = IOUtilities.streamToBytes(is);
			sOut = new String(data, 0, data.length);
		}catch(Exception e)
		{
			
		}
		return sOut;
	}
	
	public static String getOnlyLine(String sIn, int iPos)
	{
		String sOut = null;
		//Мы ищем \n или \r?
		int iPosRight = sIn.indexOf('\n', iPos);
		int iPosLeft = iPos;
		while(iPosLeft>=0)
		{
			if (sIn.charAt(iPosLeft)=='\n')
			{
				iPosLeft++;
				break;
			}
			iPosLeft--;
		}
		if (iPosLeft < 0) iPosLeft = 0;
		if (iPosLeft == iPosRight || iPosLeft == iPosRight-1) return null;
		return sIn.substring(iPosLeft, iPosRight-1);
	}
	
	public static String[] getTextFileLines(byte[] data)
	{
		// read the file into a byte array 'data'; then:
		Vector lines = new Vector();
		boolean endCR = false;
		int start = 0;
		for (int i = start; i < data.length; ++i) {
		  if (data[i] == 0xD) {
		    lines.addElement(new String(data, start, i - start));
		    endCR = true;
		    start = i + 1;
		  } else if (data[i] == 0xA) {
		    if (endCR && i == start) {
		      start++;
		      endCR = false;
		    } else {
		      lines.addElement(new String(data, start, i - start));
		      start = i + 1;
		    }
		    endCR = false;
		  }
		}
		String[] results = new String[lines.size()];
		lines.copyInto(results);
		return results;
	}

	public static String[] readTextFile(String fName) {
	 String[] result = null;
	 FileConnection fconn = null;
	 InputStream is = null; // NOT DataInputStream
	 try {
	   fconn = (FileConnection) Connector.open(fName, Connector.READ);
	   is = fconn.openDataInputStream();
	   byte[] data = IOUtilities.streamToBytes(is);
	   // result = new String(data);
	   result = getTextFileLines(data);
	 } catch (IOException e) {
	   //System.out.println(e.getMessage());
	 } finally {
	   try {
	    if (null != is)
	     is.close();
	    if (null != fconn)
	     fconn.close();
	    } catch (IOException e) {
	     //System.out.println(e.getMessage());
	    }
	 }
	 return result;
	}

	public static String[] readResourceFile(String sClass, String sFile)
	{
		String[] sRet = null;
		try{
			Class clCurrent = null;
	    	try
	    	{
				clCurrent = Class.forName(sClass);
	    	}catch (Exception e)
	    	{
	    		
	    	}
	    	if (clCurrent == null) return null;
	    	
			InputStream inputStream = clCurrent.getResourceAsStream(sFile);
			if (null != inputStream)
			{
				sRet = readResourceFile(inputStream);
				inputStream.close();
			}

		}catch(Exception e)
		{
			
		}
		return sRet;
	}
	
	public static String[] readResourceFile(InputStream is) {
		 String[] result = null;
		 FileConnection fconn = null;
		 try {
		   byte[] data = IOUtilities.streamToBytes(is);
		   // result = new String(data);
		   result = getTextFileLines(data);
		 } catch (IOException e) {
		   //System.out.println(e.getMessage());
		 } finally {
		   try {
		    if (null != is)
		     is.close();
		    if (null != fconn)
		     fconn.close();
		    } catch (IOException e) {
		     //System.out.println(e.getMessage());
		    }
		 }
		 return result;
		}
	
	//Identifies the substrings in a given string that are delimited
	//by one or more characters specified in an array, and then
	//places the substrings into a String array.

	public static String[] split(String strString, String strDelimiter) {
		String[] strArray;
		int iOccurrences = 0;
		int iIndexOfInnerString = 0;
		int iIndexOfDelimiter = 0;
		int iCounter = 0;

		//Check for null input strings.
		if (strString == null) {
			throw new IllegalArgumentException("Input string cannot be null.");
		}

		//If strString begins with delimiter then remove it in order
		//to comply with the desired format.
		if (strString.startsWith(strDelimiter)) {
			strString = strString.substring(strDelimiter.length());
		}

		//If strString does not end with the delimiter then add it
		//to the string in order to comply with the desired format.
		if (!strString.endsWith(strDelimiter)) {
			strString += strDelimiter;
		}

		//Count occurrences of the delimiter in the string.
		//Occurrences should be the same amount of inner strings.
		while((iIndexOfDelimiter = strString.indexOf(strDelimiter,
			   iIndexOfInnerString)) != -1)
		{
			iOccurrences += 1;
			iIndexOfInnerString = iIndexOfDelimiter + strDelimiter.length();
		}

		//Declare the array with the correct size.
		strArray = new String[iOccurrences];

		//Reset the indices.
		iIndexOfInnerString = 0;
		iIndexOfDelimiter = 0;

		//Walk across the string again and this time add the
		//strings to the array.
		while((iIndexOfDelimiter = strString.indexOf(strDelimiter,
			   iIndexOfInnerString)) != -1)
		{
			//Add string to array.
			strArray[iCounter] = strString.substring(iIndexOfInnerString,iIndexOfDelimiter);
			//Increment the index to the next character after
			//the next delimiter.
			iIndexOfInnerString = iIndexOfDelimiter + strDelimiter.length();
			//Inc the counter.
			iCounter += 1;
		}

		return strArray;
	}