GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Java.io.InputStream Class in Java - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Java.io.InputStream Class in Java

Last Updated : 28 Mar, 2024

Java InputStream class is the superclass of all the io classes i.e. representing an input stream of bytes. It represents an input stream of bytes. Applications that are defining a subclass of the Java InputStream class must provide a method, that returns the next byte of input. A reset() method is invoked which re-positions the stream to the recently marked position.

Input_Stream-in-Java

Declaration of Java InputStream Class

public abstract class InputStream
extends Object
implements Closeable

Constructor of InputStream Class in Java

There is a constructor used with InputStream is mentioned below:

  • InputStream(): Single Constructor

Methods of Java InputStream Class

MethodDescription
mark()marks the current position of the input stream. It sets readlimit i.e. maximum number of bytes that can be read before the mark position becomes invalid.a 
read()reads next byte of data from the Input Stream
close()closes the input stream and releases system resources associated with this stream to Garbage collector.
read()reads number of bytes of arg.length from the input stream to the buffer array arg. The bytes read by read() method are returned as an int.
reset()invoked by mark() method. It repositions the input stream to the marked position.
markSupported()              checks whether the input stream is supporting the mark() and reset() method or not.
skip()skips and discards arg bytes in the input stream.

1. mark()

Java.io.InputStream.mark(int arg) marks the current position of the input stream. It sets readlimit i.e. maximum number of bytes that can be read before mark position becomes invalid. 

Syntax :public void mark(int arg)

Parameters :
arg : integer specifying the read limit of the input Stream

Return :
void

2. read()

java.io.InputStream.read() reads next byte of data from the Input Stream. The value byte is returned in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. 

Syntax :public abstract int read()

Parameters :
------

Return :
Reads next data else, -1 i.e. when end of file is reached.

Exception :
-> IOException : If I/O error occurs.

3. close()

java.io.InputStream.close() closes the input stream and releases system resources associated with this stream to Garbage Collector. 

Syntax :public void close()

Parameters :
------

Return :
void

Exception :
-> IOException : If I/O error occurs.

4. read() : 

Java.io.InputStream.read(byte[] arg) reads number of bytes of arg.length from the input stream to the buffer array arg. The bytes read by read() method are returned as int. If len is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. 

Syntax : public int read(byte[] arg)

Parameters :
arg : array whose number of bytes to be read

Return :
reads number of bytes and return to the buffer else, -1 i.e. when end of file is reached.

Exception :
-> IOException : If I/O error occurs.
-> NullPointerException : if arg is null.

5. reset() : 

Java.io.InputStream.reset() is invoked by mark() method. It repositions the input stream to the marked position. 

Syntax :public void reset()

Parameters :
----

Return :
void

Exception :
-> IOException : If I/O error occurs.

6. markSupported() :

Java.io.InputStream.markSupported() method tests if this input stream supports the mark and reset methods. The markSupported method of InputStream returns false by default. 

Syntax :public boolean markSupported()

Parameters :
-------

Return :
true if input stream supports the mark() and reset() method else,false

7. skip() : 

Java.io.InputStream.skip(long arg) skips and discards arg bytes in the input stream. 

Syntax :public long skip(long arg)

Parameters :
arg : no. of bytes to be skipped

Return :
skip bytes.

Exception :
-> IOException : If I/O error occurs.
Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.