Results 1 to 4 of 4
  1. #1
    aytidaalkuhs is offline Member
    Join Date
    Apr 2008
    Posts
    3
    Rep Power
    0

    Default [SOLVED] file i/o problem

    Hi.
    I was writing a small code to facilitate text file reading and used the following code to read and display the text file.


    while(handle.read()!= -1)
    {
    System.out.print((char)handle.read());
    }
    where handle is an object of FileInputStream class.
    What it does is print the alternate charcters in the file starting from the second character.
    Why is it behaving in such a manner? Please throw some light.

    Alternatively, i used a code snippet from java 2 complete reference which is working just fine.

    What i want to know is whats the problem with my logic or code and not interested in getting any new ways to do it cuz i already have a correct way to do it at hand.

    Waiting..
    aytidaalkuhs.

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Welcome to the forums aytidaalkuhs!

    Please remember to use [ code] [ /code] tags when posting code.

    It's printing alternating characters from the second on because you made one call from within the while line, and another during the output line. See the API for more details. Take a view at the code too:
    Java Code:
     
    // [B]read[/B] reads from the input stream
    while(handle.read()!= -1) { // with this, you've made one read here, comparing every other read to -1
      // with this, you've made another read, outputting every other character
      System.out.print((char)handle.read()); 
    }
    My suggestion is you need a new way to do it, because if your intention is to print every char in the stream- well, this code just isn't working, now is it?
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  3. #3
    aytidaalkuhs is offline Member
    Join Date
    Apr 2008
    Posts
    3
    Rep Power
    0

    Default thanks

    thank you captain for the solution.
    actually, after coding for abt 2 hrs as a noob(in java), i couldnt even see that i was calling the handle.read() twice. Really, i can be dumb at times.
    Thanks anyways and ill keep in mind to use <code> </code> in all later posts.

  4. #4
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    No problem. We all have our moments. I've stared at code for hours on end and all the while I couldn't actually see the = when it should have been ==

    :)
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

Similar Threads

  1. [SOLVED] Delete Current line from file
    By Azndaddy in forum New To Java
    Replies: 2
    Last Post: 04-06-2012, 08:00 AM
  2. [SOLVED] Reading a text file into an Array
    By DonCash in forum New To Java
    Replies: 13
    Last Post: 01-25-2011, 12:51 AM
  3. [SOLVED] alignment problem
    By nanimtech in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-10-2008, 01:23 PM
  4. [SOLVED] Actionevent problem
    By Cymro in forum New To Java
    Replies: 3
    Last Post: 04-04-2008, 07:11 AM
  5. [SOLVED] getting values from a text file
    By dav9999 in forum New To Java
    Replies: 8
    Last Post: 04-01-2008, 01:51 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
  •