• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

read() method

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Why is the word "abbreviation" so long? And this ad is so short?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic