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
Code :
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
Re: Problem understanding read method.
Quote:
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