BlackBerry Forums Support Community

BlackBerry Forums Support Community (
-   Developer Forum (
-   -   Reading from a text file (

kmdcomp 08-13-2008 06:46 AM

Reading from a text file
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.

irlennard 08-15-2008 06:36 AM

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

KeniF 08-15-2008 08:47 AM

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



All times are GMT -5. The time now is 08:40 AM.

Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.