my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes help with simple IO code ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "help with simple IO code ?" Watch "help with simple IO code ?" New topic
Author

help with simple IO code ?

mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
what gets printed out is all fine, except that ? gets printed at the end.
what is this and how to get rid of this ?
here is code snippet:
while(c != -1)
{c=in.read();ch=(char)c;System.out.print(ch);}
where "in" is FileInputStream
[ February 27, 2002: Message edited by: mark stone ]
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
hi mark.
well i dont know about the ? part but the loop is wrong in a few ways:
1) FileInputStream reads bytes, not chars. so u cant convert each byte to char and expect it to print the right thing.
2) the while loop doesnt end EXACTLY at the end of the file. look at what it does:
it reads a byte, prints it and then checks to see it it is -1. so when it reaches -1 (the end) it will still print it!
thats why for IO loops are usually written like this:
while((c=in.read())!=-1)
so here, u read a byte and imeediatly check to see if it is -1 before printing it.
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
sounds correct.
but what is character ?
it is 2 bytes. rights ? and we are reading one byte at a time. and most characters like A is 65, B is 66 etc are below 127, so the byte that is read would be interpreted ok ?
but as i ammended the while statement as you said, the ? disappeared !!!
but the code is now printing characters haphazard manner. earlier i could printout the contents of the file fine, and only the ? was appearing at the end !!, now the characters are being printed jumbled.
i guess i'll have to research a bit more on this. meanwhile if you or someone has any more insights please let me know.
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
you are right that most charaters fit in a byte, but some doesnt. remember that java uses UNICODE for the charactes and thats why each char takes 16 bit rather than 8 (like most other languages).
because of this big problem, java made a lot of classes dealing specifically with charactes stream RATHER than byte streams.
so if you are trying to read txt files of any sort dont use those streams that deal with bytes. use those that read chars!.
all the streams dealing with chars are named Reader and Writer accordinly.
so instead of FileInputStream, use a FileReader.
that will solve your problem (btw, their usage is exactly the same).
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
I am now using FileReader. But now some words are not being printed ? why ? some words are being skipped ?

am i doing something incorrect here. can someone please correct ? or do i need a buffer here ?
FileReader fr = new FileReader(inputFile);
while((c=fr.read()) != -1)
{c=fr.read();System.out.print((char)c);}
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
FileReader fr = new FileReader(inputFile);
while((c=fr.read()) != -1)
{c=fr.read();System.out.print((char)c);}
In your while-test, you read a character from the Reader, then compare it to -1...if it's NOT -1, you execute your block. The first thing you do is...read another character!! Where did the character you just read (the one you compared to -1) go? To char heaven maybe? You're basically loosing a character each time through the loop. your c=fr.read() in your block is redundant. All you need is the one in the while-test.


Rob
SCJP 1.4
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
thanks Rob.
 
Consider Paul's rocket mass heater.
 
subject: help with simple IO code ?
 
Similar Threads
Method Invocation..
'if'block ??
insertion sort
This is from Javaprepare.com
Java program Grade