Results 1 to 7 of 7

Thread: Read Text File

  1. #1
    Blackberrylerner is offline Senior Member
    Join Date
    Jul 2010
    Posts
    101
    Rep Power
    0

    Default Read Text File

    Hi All

    I am using J2ME and BlackBerry JDE,I try to read Text file from Pc(Example C:\Test.txt).
    Can any one give me example

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    I don't really know much about specific APIs, but standard java comes with a Scanner class that's pretty usefull for such tasks. Try to find an equivalent in your enviroment.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    Blackberrylerner is offline Senior Member
    Join Date
    Jul 2010
    Posts
    101
    Rep Power
    0

    Default

    Quote Originally Posted by m00nchile View Post
    I don't really know much about specific APIs, but standard java comes with a Scanner class that's pretty usefull for such tasks. Try to find an equivalent in your enviroment.
    Thanks for reply i have manage to read file but one more may it's a very common or normal.When i read file then code going in infinite loop.
    Java Code:
     
    try {
    
       Class classs = Class.forName("com.rim.samples.device.smsdemo.SmsTest");
    
         InputStream is = classs.getResourceAsStream("/test.txt");
         InputStreamReader isr = new InputStreamReader(is);
    
             char c;
               while ((c = (char)isr.read()) != -1) 
               {
                   
                    System.out.print(c);
                    
                }
    
            } catch(Exception ex) {
    
                System.out.println("Error: " + ex.toString());
    
            }
    In Test.txt file i have only two line
    check
    Test

    Please help me

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    You neglected to tell us what gets printed inside the infinite loop.

    db

  5. #5
    Blackberrylerner is offline Senior Member
    Join Date
    Jul 2010
    Posts
    101
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    You neglected to tell us what gets printed inside the infinite loop.

    db
    Sorry
    Out show blank
    Java Code:
    '￿'

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Blackberrylerner View Post
    Thanks for reply i have manage to read file but one more may it's a very common or normal.When i read file then code going in infinite loop.
    Java Code:
     
    try {
    
       Class classs = Class.forName("com.rim.samples.device.smsdemo.SmsTest");
    
         InputStream is = classs.getResourceAsStream("/test.txt");
         InputStreamReader isr = new InputStreamReader(is);
    
             char c;
               while ((c = (char)isr.read()) != -1) 
               {
                   
                    System.out.print(c);
                    
                }
    
            } catch(Exception ex) {
    
                System.out.println("Error: " + ex.toString());
    
            }
    In Test.txt file i have only two line
    check
    Test

    Please help me
    I would do it like this:

    Java Code:
    int c;
    while ((c= isr.read()) != -1) {
       System.out.print((char)c);
    }
    casting -1 to a char has a two byte result 0xffff which isn't equal to -1 (chars are unsigned).

    kind regards,

    Jos

  7. #7
    Blackberrylerner is offline Senior Member
    Join Date
    Jul 2010
    Posts
    101
    Rep Power
    0

    Default

    Thanks it's working
    Last edited by Blackberrylerner; 08-14-2010 at 11:51 AM.

Similar Threads

  1. Read text file
    By Sean04 in forum New To Java
    Replies: 18
    Last Post: 06-16-2010, 01:43 AM
  2. Can't read from a text file
    By kevin8160 in forum New To Java
    Replies: 2
    Last Post: 02-07-2010, 06:45 PM
  3. Read from a text file?
    By aaronfsimons in forum New To Java
    Replies: 3
    Last Post: 05-01-2009, 04:42 AM
  4. read from text file
    By rayda in forum New To Java
    Replies: 5
    Last Post: 04-10-2009, 03:51 AM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •