Java.io - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io InputStream

Java.io InputStream

shape Introduction

The Java.io InputStream is one of the supper class to all types of input streams in java.io package. Generally the input stream acts as a source to the data like file or connection.

shape Conceptual figure

The ByteArrayInputAtream, FileInputStream, FilterInputStream, are the subclass of input stream and shown in detail in the above figure.

shape Description

The methods inherited mainly from the InputStream class to subclasses are read(), mark() and reset(). All the subclasses to input stream supports all this methods.

Class Declaration

shape Declaration

Java.io InputStream class is declared below: Public abstract class InputStream extends Object implements closeable

Class Constructors

shape Table

Constructor Description
InputStream() The function of this constructor is to create the input stream instance.

Class Methods

shape Table

Method Description
void close () The function of this method is to close the present stream and associated to input stream systems resources are released without any impact.
Long skip (long m) The function of this method is to skip the text or characters in this stream of ' m ' bytes.
Boolean markSupported () The function of this method is to check the represented mark method is supported by this stream or not
int read(byte[] b, int off, int n) By using this method it reads the data up to 'n' bytes from the input stream to an array of the bytes.

Inherited Methods

shape Description

From the following classes, methods are inherited to input stream class.
  • Java.io.Object

shape Examples

Usage of void close() method. [c]import java.io.FileInputStream; import java.io.InputStream; public class InputStream { public static void main(String[] args) throws Exception { InputStream is = null; int i=0; try{ // creating a new input stream is = new FileInputStream("C://test.txt"); // To invoke available i = is.available(); // The number of bytes which are available are printed System.out.println(i); // It releases any system resources associated with this stream is.close(); // It throws I/O exception on available() invocation i = is.available(); System.out.println(i); }catch(Exception e) { // If any I/O error occurs System.out.print("input stream is closed"); }finally { // releases all the system resources associated with this stream if(is!=null) is.close(); } } }[/c] Output The result will be as follows. [c]input stream is closed[/c] Usage of Long skpip (Long n) method. [c]import java.io.FileInputStream; import java.io.InputStream; public class InputStream { public static void main(String[] args) throws Exception { InputStream is = null; int i ; char c; try{ // creating a new input stream is = new FileInputStream("D://test.txt"); while((i=is.read())!=-1) { // To convert integer to character c=(char)i; // To print character System.out.println("Read Character: "+c); // To skip one byte is.skip(1); } }catch(Exception e) { // if any I/O error occurs e.printStackTrace(); }finally { // releases all the system resources associated with this stream if(is!=null) is.close(); } } }[/c] Output Following is the result will be displayed. [c] Read Character: o Read Character: s Read Character: c Read Character: m[/c]

Summary

shape Key Points

    <liThe Java.io InputStream is one of the supper class to all types of input streams in java.io package.
  • Generally the Java.io InputStream acts as a source to the data like file or connection.
  • The methods inherited mainly from the InputStream class to subclasses are read(), mark() and reset().