BlackBerry Forums Support Community
              

Closed Thread
 
Thread Tools
Old 08-13-2008, 06:46 AM   #1
kmdcomp
New Member
 
Join Date: Apr 2008
Model: 8830
PIN: N/A
Carrier: verizon
Posts: 9
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
irlennard
Knows Where the Search Button Is
 
irlennard's Avatar
 
Join Date: Jan 2008
Location: Berlin
Model: 9000
Carrier: T-Mobile
Posts: 32
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
KeniF
New Member
 
Join Date: Aug 2008
Model: 7100T
PIN: N/A
Carrier: none
Posts: 1
Default

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

sb.append((char)c);

}
Offline  
Closed Thread



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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


LED AC Electric Voltage Power Detector Sensor Tester Non-Contact Pen 12-1000V picture

LED AC Electric Voltage Power Detector Sensor Tester Non-Contact Pen 12-1000V

$12.49



USB PD Battery Power Tester Digital Type C Multimeter Voltmeter Voltage Current picture

USB PD Battery Power Tester Digital Type C Multimeter Voltmeter Voltage Current

$18.90



AVR SX460 Automatic Voltage Volt Regulator Replacement For Stamford Generator picture

AVR SX460 Automatic Voltage Volt Regulator Replacement For Stamford Generator

$17.18



100A AC Meter Ammeter Volt Energy Voltage Power LCD Display Monitor Panel picture

100A AC Meter Ammeter Volt Energy Voltage Power LCD Display Monitor Panel

$17.09



Volt Gauge Meter Voltage LED Digital Display DC 12V-24V Car Panel Voltmeter picture

Volt Gauge Meter Voltage LED Digital Display DC 12V-24V Car Panel Voltmeter

$9.95



12V Digital LED Display Voltmeter Voltage Gauge Panel Meter for Car Motorcycle picture

12V Digital LED Display Voltmeter Voltage Gauge Panel Meter for Car Motorcycle

$6.49







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