🌐
Programiz
programiz.com › java-programming › inputstream
Java InputStream (With Example)
In this tutorial, we will learn about the Java InputStream class and its methods with the help of an example. The InputStream class of the java.io package is an abstract superclass that represents an input stream of bytes.
🌐
Jenkov
jenkov.com › tutorials › java-io › inputstream.html
Java InputStream
The read() method of an InputStream returns an int which contains the byte value of the byte read. Here is an InputStream read() example: ... To read all bytes in a Java InputStream you must keep reading until the value -1 is returned.
🌐
Tutorialspoint
tutorialspoint.com › java › io › java_io_inputstream.htm
Java - InputStream Class
The following example shows the usage of Java InputStream close() method. package com.tutorialspoint; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class InputStreamDemo { public static void main(String[] args) { try (InputStream inputStream = new FileInputStream("example.txt")) { int data; while ((data = inputStream.read()) != -1) { // Read character by character System.out.print((char) data); } // No need to manually close, try-with-resources handles it } catch (IOException e) { e.printStackTrace(); } } }
🌐
ZetCode
zetcode.com › java › inputstream
Java InputStream - reading data with Java InputStream
February 21, 2024 - Java InputStream tutorial shows how to work with InputStream class in Java. We work with FileInputStream, ObjectOutputStream, and SequenceInputStream subclasses.
🌐
W3Schools
w3schools.com › java › java_fileinputstream.asp
Java FileInputStream
HTML Certificate CSS Certificate JavaScript Certificate Front End Certificate SQL Certificate Python Certificate PHP Certificate jQuery Certificate Java Certificate C++ Certificate C# Certificate XML Certificate ... W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-io-inputstream-class-in-java
Java.io.InputStream Class in Java - GeeksforGeeks
March 28, 2024 - 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 · Java.io.InputStream.skip(long arg) skips and discards arg bytes in the input stream.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › io › InputStream.html
InputStream (Java Platform SE 7 )
The read(b, off, len) method for class InputStream simply calls the method read() repeatedly. If the first such call results in an IOException, that exception is returned from the call to the read(b, off, len) method.
🌐
Tutorialspoint
tutorialspoint.com › java › io › inputstream_read.htm
Java - InputStream read() method
The following example shows the usage of Java InputStream read() method. package com.tutorialspoint; import java.io.FileInputStream; import java.io.InputStream; public class InputStreamDemo { public static void main(String[] args) throws Exception { InputStream is = null; int i; char c; try { // new input stream created is = new FileInputStream("test.txt"); System.out.println("Characters printed:"); // reads till the end of the stream while((i = is.read())!=-1) { // converts integer to character c = (char)i; // prints character System.out.print(c); } } catch(Exception e) { // if any I/O error occurs e.printStackTrace(); } finally { // releases system resources associated with this stream if(is!=null) is.close(); } } }
🌐
Princeton
cs.princeton.edu › courses › archive › spr96 › cs333 › java › tutorial › java › io › overview.html
Overview of java.io's Input and Output Streams
The first of the following two diagrams shows the class hierarchy for the input stream classes comprising the java.io package. InputStream inherits from the Object class; six classes inherit directly from InputStream. One of OutputStream's descendents, FilterInputStream, is itself an abstract ...
Find elsewhere
🌐
LabEx
labex.io › tutorials › java-how-to-manage-input-streams-in-java-418035
How to manage input streams in Java | LabEx
long bytesSkipped = inputStream.skip(100); // Skip 100 bytes ... Enhance your stream handling skills with practical exercises on LabEx! graph LR A[Input Stream] --> B[Buffered Stream] B --> C[Data Stream] C --> D[Processing] import java.io.*; public class StreamChainingDemo { public static void main(String[] args) { try ( FileInputStream fis = new FileInputStream("/home/labex/data.bin"); BufferedInputStream bis = new BufferedInputStream(fis); DataInputStream dis = new DataInputStream(bis) ) { // Read different data types int intValue = dis.readInt(); double doubleValue = dis.readDouble(); String stringValue = dis.readUTF(); } catch (IOException e) { e.printStackTrace(); } } }
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › io › InputStream.html
InputStream (Java SE 21 & JDK 21)
January 20, 2026 - The read(b, off, len) method for class InputStream simply calls the method read() repeatedly. If the first such call results in an IOException, that exception is returned from the call to the read(b, off, len) method.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › InputStream.html
InputStream (Java Platform SE 8 )
March 16, 2026 - The read(b, off, len) method for class InputStream simply calls the method read() repeatedly. If the first such call results in an IOException, that exception is returned from the call to the read(b, off, len) method.
🌐
Baeldung
baeldung.com › home › java › java io › java – convert file to inputstream
Java - Convert File to InputStream | Baeldung
January 5, 2024 - In this quick tutorial, we’re going to show how to convert a File to an InputStream — first using plain Java and then Guava and the Apache Commons IO library.
🌐
GeeksforGeeks
geeksforgeeks.org › java › implement-how-to-load-file-as-inputstream-in-java
Implement how to load File as InputStream in Java - GeeksforGeeks
July 23, 2025 - The read() method of InputStream class, reads a byte of data from the input stream. The next byte of data is returned, or -1 if the end of the file is reached and throws an exception if an I/O error occurs.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › io › InputStream.html
InputStream (Java SE 17 & JDK 17)
January 20, 2026 - The read(b, off, len) method for class InputStream simply calls the method read() repeatedly. If the first such call results in an IOException, that exception is returned from the call to the read(b, off, len) method.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › io › InputStream.html
InputStream (Java SE 11 & JDK 11 )
January 20, 2026 - The read(b, off, len) method for class InputStream simply calls the method read() repeatedly. If the first such call results in an IOException, that exception is returned from the call to the read(b, off, len) method.
🌐
Oracle
docs.oracle.com › javase › 10 › docs › api › java › io › InputStream.html
InputStream (Java SE 10 & JDK 10 )
The read(b, off, len) method for class InputStream simply calls the method read() repeatedly. If the first such call results in an IOException, that exception is returned from the call to the read(b, off, len) method.
🌐
Scaler
scaler.com › home › topics › input stream in java
Input Stream in Java - Scaler Topics
October 17, 2022 - Printing the InputStream. ... Java Program to Convert String to InputStream using ByteArrayInputStream.
🌐
Smartprogramming
smartprogramming.in › tutorials › java › inputstream-classes-guide
Java InputStream Classes Guide
Learn all Java InputStream classes with detailed explanation, examples, and usage. Understand FileInputStream, BufferedInputStream, DataInputStream, ObjectInputStream, and more.
Top answer
1 of 9
247

The goal of InputStream and OutputStream is to abstract different ways to input and output: whether the stream is a file, a web page, or the screen shouldn't matter. All that matters is that you receive information from the stream (or send information into that stream.)

InputStream is used for many things that you read from.

OutputStream is used for many things that you write to.

Here's some sample code. It assumes the InputStream instr and OutputStream osstr have already been created:

int i;

while ((i = instr.read()) != -1) {
    osstr.write(i);
}

instr.close();
osstr.close();
2 of 9
97

InputStream is used for reading, OutputStream for writing. They are connected as decorators to one another such that you can read/write all different types of data from all different types of sources.

For example, you can write primitive data to a file:

File file = new File("C:/text.bin");
file.createNewFile();
DataOutputStream stream = new DataOutputStream(new FileOutputStream(file));
stream.writeBoolean(true);
stream.writeInt(1234);
stream.close();

To read the written contents:

File file = new File("C:/text.bin");
DataInputStream stream = new DataInputStream(new FileInputStream(file));
boolean isTrue = stream.readBoolean();
int value = stream.readInt();
stream.close();
System.out.printlin(isTrue + " " + value);

You can use other types of streams to enhance the reading/writing. For example, you can introduce a buffer for efficiency:

DataInputStream stream = new DataInputStream(
    new BufferedInputStream(new FileInputStream(file)));

You can write other data such as objects:

MyClass myObject = new MyClass(); // MyClass have to implement Serializable
ObjectOutputStream stream = new ObjectOutputStream(
    new FileOutputStream("C:/text.obj"));
stream.writeObject(myObject);
stream.close();

You can read from other different input sources:

byte[] test = new byte[] {0, 0, 1, 0, 0, 0, 1, 1, 8, 9};
DataInputStream stream = new DataInputStream(new ByteArrayInputStream(test));
int value0 = stream.readInt();
int value1 = stream.readInt();
byte value2 = stream.readByte();
byte value3 = stream.readByte();
stream.close();
System.out.println(value0 + " " + value1 + " " + value2 + " " + value3);

For most input streams there is an output stream, also. You can define your own streams to reading/writing special things and there are complex streams for reading complex things (for example there are Streams for reading/writing ZIP format).