Results 1 to 4 of 4
Thread: [SOLVED] file i/o problem
- 04-06-2008, 07:20 AM #1
Member
- Join Date
- Apr 2008
- Posts
- 3
- Rep Power
- 0
[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.
- 04-06-2008, 10:52 AM #2
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:
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?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()); }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)
- 04-06-2008, 06:17 PM #3
Member
- Join Date
- Apr 2008
- Posts
- 3
- Rep Power
- 0
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.
- 04-06-2008, 06:42 PM #4
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
-
[SOLVED] Delete Current line from file
By Azndaddy in forum New To JavaReplies: 2Last Post: 04-06-2012, 08:00 AM -
[SOLVED] Reading a text file into an Array
By DonCash in forum New To JavaReplies: 13Last Post: 01-25-2011, 12:51 AM -
[SOLVED] alignment problem
By nanimtech in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 04-10-2008, 01:23 PM -
[SOLVED] Actionevent problem
By Cymro in forum New To JavaReplies: 3Last Post: 04-04-2008, 07:11 AM -
[SOLVED] getting values from a text file
By dav9999 in forum New To JavaReplies: 8Last Post: 04-01-2008, 01:51 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks