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
New Contributor
Posts: 8
Registered: ‎10-19-2011
My Device: 9700
My Carrier: T-Mobile
Accepted Solution

Wrong Output from text-file

I´d like to read a textfile from the SD-Card and show the content.

 

The file-content:

Archer 1
Archer 2
Archer 3
Archer 4

 my code:

		try {
			FileConnection con = (FileConnection) javax.microedition.io.Connector.open("file:///SDCard/test.txt");
			if(con.exists())
			{
				InputStream input = con.openInputStream();
				StringBuffer buffer = new StringBuffer();
				while(input.read() != -1)
				{
					buffer.append(input.read());
				}
				showMessage("OutPut: "+buffer.toString());
			}
		} catch (IOException e) {
			showMessage("Es ist ein Fehler aufgetreten.");
		}

 

And the ouput is:

11410411449101141041145010114104114511011410411452

 

What has to be changed?

 

Highlighted
Developer
Posts: 176
Registered: ‎11-06-2008
My Device: Not Specified

Re: Wrong Output from text-file

The problem is you are reading the file as bytes and the StringBuffer does not accept bytes as something you can append. Therefore it's interpretting the byte value read from the InputStream as a raw integer.

 

Two quick solutions off the top of my head are as follows, I haven't tested this code. The first one reads the InputStream using an InputStreamReader which can give you the file contents as a char array which can be appended directly to the StringBuffer. The second way still reads the file as bytes but then uses the StringUtilities class to append the byte array to the StringBuffer.

try 
{
    FileConnection con = (FileConnection) javax.microedition.io.Connector.open("file:///SDCard/test.txt");
    if(con.exists())
    {
        InputStream input = con.openInputStream();
        InputStreamReader reader = new InputStreamReader(input);
        StringBuffer buffer = new StringBuffer();

        char[] charBuffer = new char[1024 * 8];
        int lengthRead;
        while ((lengthRead = reader.read(charBuffer, 0, charBuffer.length)) > 0)
        {
            buffer.append(charBuffer, 0, lengthRead);
        }
        showMessage("OutPut: "+buffer.toString());
    }
} 
catch (IOException e) 
{
    showMessage("Es ist ein Fehler aufgetreten.");
}





try 
{
    FileConnection con = (FileConnection) javax.microedition.io.Connector.open("file:///SDCard/test.txt");
    if(con.exists())
    {
        InputStream input = con.openInputStream();
        StringBuffer buffer = new StringBuffer();

        byte[] byteBuffer = new byte[1024 * 8];
        int lengthRead;
        while ((lengthRead = input.read(byteBuffer, 0, byteBuffer.length)) > 0)
        {
            StringUtilities.append(buffer, byteBuffer, 0, lengthRead);
        }
        showMessage("OutPut: "+buffer.toString());
    }
} 
catch (IOException e) 
{
    showMessage("Es ist ein Fehler aufgetreten.");
}

 

New Contributor
Posts: 8
Registered: ‎10-19-2011
My Device: 9700
My Carrier: T-Mobile

Re: Wrong Output from text-file

Both versions are functioning well!

 thx