jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes read() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "read() method" Watch "read() method" New topic
Author

read() method

Sue Pillai
Ranch Hand

Joined: Nov 04, 2004
Posts: 89
Hi,

This code echoes whatever you type on the system console:

InputStream iStream;
OutputStream oStream;
int c;

iStream=System.in;
oStream=System.out;
System.out.println("Type Some Characters");

try
{
while ((c=iStream.read())!=-1)
oStream.write(c);
}
catch (IOException e)
{
System.out.println("Error:" + e.getMessage());
}
finally
{
iStream.close();
oStream.close();
}

I understand that InputStream is an abstract class and read() is the abstract method. So, how am I being able to use the abstract read() method to read the console input? Which class is implementing the method?

Thanks


Sue

SCJP 1.2
SCWCD 1.4
SCBCD 5.0
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Although the variable "in" is declared to be of type InputStream, it can point to an object of any class that directly or indirectly extends InputStream. The precise name of the class that's used as the System.in object is implementation-specific. If you want to know what it is on your Java implementation, just out of curiosity, just do something like

System.out.println(System.in.getClass().getName());

This will print the name of the class of the System.in object.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: read() method
 
Similar Threads
Abstract Method
I am able to display the file information, but I am having problem displaying the employee details.
OutOfMemoryExcepiton!
Data lost when writing object to a file
sending attachments using javamail