Input Stream
The Java InputStream class is the superclass of all input streams. The
input stream is used to read data from numerous input devices like the
keyboard, network, etc. InputStream is an abstract class, and because of
this, it is not useful by itself. However, its subclasses are used to read
data.
There are several subclasses of the InputStream class, which are as
follows:
1. AudioInputStream
2. ByteArrayInputStream
3. FileInputStream
4. FilterInputStream
5. StringBufferInputStream
6. ObjectInputStream
7. DataInputStream
1.AudioInputStream
Part of javax.sound.sampled package.
Used for handling audio data, allowing you to read audio files or streams for
playback, processing, or conversion.
2.ByteArrayInputStream
Part of java.io package.
Allows an application to read data from a byte array as an input stream. Useful when
you have data in memory that you want to process as a stream.
3.FileInputStream
Part of java.io package.
Used to read bytes from a file. Ideal for reading binary data like images, audio files,
etc.
4.FilterInputStream
Part of java.io package.
A superclass for all classes that filter input streams. It’s typically used as a base class
to create streams that modify or enhance the data being read, like
BufferedInputStream.
5.StringBufferInputStream
Deprecated as of Java 1.4.
Previously used to read from a StringBuffer as an input stream. Replaced by
ByteArrayInputStream or other more flexible classes.
6. ObjectInputStream
Part of java.io package.
Used to deserialize objects—reading Java objects from an input stream. Commonly
used in Java RMI, or for object persistence.
7. DataInputStream is a class that allows an application to read Java primitive data types
(like int, float, double, char, etc.) from an underlying input stream in a machine-
independent way. This is particularly useful when you're working with binary data streams.
Key Features:
Reading Primitive Data Types: It supports reading data types like int, float, long,
double, UTF, and more.
Machine-Independent: Ensures data is read in the same way across different
platforms.
Common Methods:
readInt(): Reads 4 bytes and converts them to an int.
readFloat(): Reads 4 bytes and converts them to a float.
readDouble(): Reads 8 bytes and converts them to a double.
readUTF(): Reads a string encoded in modified UTF-8.
Output Stream
The output stream is used to write data to numerous output devices like
the monitor, file, etc. OutputStream is an abstract superclass that
represents an output stream. OutputStream is an abstract class and
because of this, it is not useful by itself. However, its subclasses are
used to write data.
There are several subclasses of the OutputStream class which are as
follows:
1. ByteArrayOutputStream
2. FileOutputStream
3. StringBufferOutputStream
4. ObjectOutputStream
5. DataOutputStream
6. PrintStream
2.ByteArrayOutputStream
Part of java.io package.
Writes data to a byte array in memory. Useful for scenarios where you need to capture
output as bytes, like when working with network protocols or image processing.
3.FileOutputStream
Part of java.io package.
Writes bytes to a file. Ideal for writing binary data such as images, audio, or any raw
data that doesn’t require text encoding.
4.StringBufferOutputStream
Deprecated since Java 1.4.
Was used to write data to a StringBuffer. Replaced by more flexible classes like
ByteArrayOutputStream or using StringBuilder for string manipulations.
5.ObjectOutputStream
Part of java.io package.
Serializes Java objects—writes objects to an output stream for later reconstruction
(deserialization) using ObjectInputStream.
6.DataOutputStream
Part of java.io package.
Allows you to write Java primitive data types (like int, float, double, etc.) in a
machine-independent way. Useful for creating custom binary file formats.
7.PrintStream
Part of java.io package.
Provides methods to write formatted text data easily. Often used for console output
(System.out) but can also write to files or other output streams.
Write a Java program to open a file and display the contents in the console
window.
import java.io.*;
public class Prac8a {
public static void main(String[] args) throws FileNotFoundException, IOException {
InputStream is=new FileInputStream("D:\\Core Java\\Demo1.txt");
int count= is.available();
byte a[]=new byte[count];
is.read(a);
for (byte b : a) {
char k=(char)b;
System.out.print(k+"-");
Write a java program to copy the contents from one file to another file.
import java.io.*;
public class prac8b {
public static void main(String[] args) throws FileNotFoundException, IOException {
FileInputStream fis=null;
FileOutputStream fos=null;
try {
File inF=new File("D:\\Core Java\\Demo1.txt");
File outF=new File("D:\\Core Java\\Demo2.txt");
fis=new FileInputStream(inF);
fos=new FileOutputStream(outF);
byte buff[]=new byte[1024];
int length;
while((length=fis.read(buff))>0)
fos.write(buff,0,length);
fis.close();
fos.close();
System.out.println("Contents Coppied...");
} catch (Exception e) {
e.printStackTrace();
}}}
Write a java program to read the student data from user and store it in the
file.
import java.io.*;
import java.util.Scanner;
public class prac8c {
public static void main(String[] args) throws FileNotFoundException, IOException {
String s1,s2,s3;
Scanner sc=new Scanner(System.in);
System.out.println("Enter name: ");
s1= sc.nextLine();
System.out.println("Enter phone number");
s2= sc.nextLine();
System.out.println("Enter address: ");
s3= sc.nextLine();
OutputStream fos=new FileOutputStream("D:\\Core Java\\Student.txt");
byte b1[]=s1.getBytes();
fos.write(b1);
byte b2[]=s2.getBytes();
fos.write(b2);
byte b3[]=s3.getBytes();
fos.write(b3);
fos.close();
System.out.println("File Created");