BlackBerry Forums Support Community

Closed Thread
LinkBack Thread Tools
Old 08-13-2008, 06:46 AM   #1 (permalink)
New Member
Join Date: Apr 2008
Model: 8830
Carrier: verizon
Posts: 9
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Reading from a text file

Please Login to Remove!

Ok, there is no way that this is as hard as I'm making it out to be.

my.txt has only one line in it that says "this is the first line".

I have tried reading the docs and writing my own code and I've tried 15 other examples that I've found on the internet and in books, nothing seems to work.

            FileConnection fconn = null;
            InputStream inputStream = null;
            StringBuffer sb = new StringBuffer();

            String filePath = "file:///SDCard/newFolder/my.txt";
                try {
                    fconn = (FileConnection);
                    inputStream = fconn.openInputStream();
                    InputStreamReader isr = new InputStreamReader(inputStream);

            char c;

            while ((c = (char) != -1) {


            return sb.toString();
        } catch(Exception ex) {
            return "problem";


The path is correct, my.txt was created by another piece of code in the program.
Old 08-15-2008, 06:36 AM   #2 (permalink)
Knows Where the Search Button Is
irlennard's Avatar
Join Date: Jan 2008
Location: Berlin
Model: 9000
Carrier: T-Mobile
Posts: 32
Post Thanks: 0
Thanked 0 Times in 0 Posts

So what is the problem? What does your code above do? One thing I noticed though, is that you're comparing the char "c" variable to -1. Char is unsigned so this will never succeed. I would suggest making c an int and doing the cast to char where you append it to your StringBuffer.

-- Ian

Last edited by irlennard : 08-15-2008 at 06:37 AM.
Old 08-15-2008, 08:47 AM   #3 (permalink)
New Member
Join Date: Aug 2008
Model: 7100T
Carrier: none
Posts: 1
Post Thanks: 0
Thanked 0 Times in 0 Posts

int c;
while ((c = != -1) {



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