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
PIN: N/A
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.

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.
Offline  
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
Default

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.
Offline  
Old 08-15-2008, 08:47 AM   #3 (permalink)
New Member
 
Join Date: Aug 2008
Model: 7100T
PIN: N/A
Carrier: none
Posts: 1
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

sb.append((char)c);

}
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.