• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

read() method

 
Sue Pillai
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic