Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Problem understanding read method.

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Problem understanding read method.

    I am confused. In the program below, the read method converts the fin object into what appears to characters. That's why the variable "i" is converted into the character type. I thought the character type could only be used to display one letter, number, or other symbol. Why then was the whole file printed to screen. Please explain to me.

    Thanks,
    Truck35

    try { 
          // read bytes until EOF is encountered 
          do { 
            i = fin.read(); 
            if(i != -1) System.out.print((char) i); 
          } while(i != -1); 
        } catch(IOException exc) {
          System.out.println("Error reading file.");
        }


    --- Update ---

    I was just wondering. The read method reads the file each byte at a time. Then it returns -1 when done or a error message if it dosn't finish. Am I able to see characters because of the conversion used in the "println" statement. Please let me know if this is true.

    Thanks,
    Truck35


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem understanding read method.

    Am I able to see characters because of the conversion used in the "println" statement.
    By conversion I assume you mean the cast: (char) which caused the print method to display the contents of the int variable as a char instead of as an int. See the ASCII character values table to see the int values for chars
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Truck35 (February 12th, 2013)

Similar Threads

  1. Replies: 1
    Last Post: January 23rd, 2013, 07:29 AM
  2. Help in understanding this recursive method
    By jameschristopher06 in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 20th, 2012, 01:23 PM
  3. problem in understanding output.
    By vinaysa86 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 5th, 2012, 01:22 AM
  4. Need help understanding math.random method.
    By slashdash in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2012, 05:50 PM
  5. Need help understanding method calls and such
    By hackman2007 in forum Java Theory & Questions
    Replies: 8
    Last Post: July 14th, 2010, 08:18 AM