BlackBerry Forums Support Community

BlackBerry Forums Support Community (http://www.blackberryforums.com/)
-   Developer Forum (http://www.blackberryforums.com/developer-forum/)
-   -   Reading from a text file (http://www.blackberryforums.com/developer-forum/144967-reading-text-file.html)

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.

Code:


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

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

            char c;

            while ((c = (char)isr.read()) != -1) {

                sb.append(c);

            }
           
            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 = isr.read()) != -1) {

sb.append((char)c);

}


All times are GMT -5. The time now is 07:52 AM.

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