codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

47 / 65 topics
43Java Files44Java Create & Write Files45Java Read Files46Java Delete Files47Java I/O Streams48FileInputStream & FileOutputStream49BufferedReader & BufferedWriter
Tutorials/Java Programming/Java I/O Streams
☕Java Programming

Java I/O Streams

Updated 2026-05-12
30 min read

Java I/O Streams

In the world of programming, handling input and output (I/O) is a fundamental task. Whether you're reading from or writing to files, network sockets, or other sources, understanding how to manage data flow efficiently is crucial. In this tutorial, we'll dive into Java's I/O streams, focusing on InputStream, OutputStream, and the differences between byte streams and character streams.

Introduction

Java provides a robust framework for handling input and output operations through its I/O classes. These classes are organized into two main categories: byte streams and character streams. Byte streams handle raw binary data, while character streams handle text data encoded in specific character sets (like UTF-8). Understanding these concepts will help you build efficient and effective Java applications that interact with various input/output sources.

Core Content

1. InputStream and OutputStream

What are InputStream and OutputStream?

  • InputStream: This abstract class is the superclass of all classes representing an input stream of bytes. It provides methods to read byte-oriented data from a source.
  • OutputStream: This abstract class is the superclass of all classes representing an output stream of bytes. It provides methods to write byte-oriented data to a destination.

Basic Methods

  • InputStream:

    • int read(): Reads the next byte of data from the input stream.
    • int read(byte[] b): Reads up to b.length bytes of data from the input stream into an array of bytes.
    • void close(): Closes the input stream and releases any system resources associated with it.
  • OutputStream:

    • void write(int b): Writes the specified byte to this output stream.
    • void write(byte[] b): Writes b.length bytes from the specified byte array to this output stream.
    • void close(): Closes this output stream and releases any system resources associated with it.

Example: Basic InputStream and OutputStream Usage

Java
1import java.io.FileInputStream;
2import java.io.FileOutputStream;
3import java.io.InputStream;
4import java.io.OutputStream;
5
6public class StreamExample {
7 public static void main(String[] args) {
8 try (InputStream in = new FileInputStream("input.txt");
9 OutputStream out = new FileOutputStream("output.txt")) {
10
11 int data;
12 while ((data = in.read()) != -1) {
13 out.write(data);
14 };
15 } catch (Exception e) {
16 e.printStackTrace();
17 }
18 }
19}
Output

Tip

Use byte streams when dealing with binary data and character streams when dealing with text data. This distinction helps avoid issues related to encoding.

Practical Example

Let's create a simple program that reads from a file, converts the content to uppercase, and writes it to another file using both byte streams and character streams.

Java
1import java.io.FileInputStream;
2import java.io.FileOutputStream;
3import java.io.FileReader;
4import java.io.FileWriter;
5import java.io.InputStream;
6import java.io.OutputStream;
7import java.io.Reader;
8import java.io.Writer;
9
10public class UppercaseConverter {
11 public static void main(String[] args) {
12 // Using byte streams
13 try (InputStream in = new FileInputStream("input.txt");
14 OutputStream out = new FileOutputStream("output_byte_uppercase.txt")) {
15
16 int data;
17 while ((data = in.read()) != -1) {
18 out.write(Character.toUpperCase((char) data));
19 }
20 } catch (Exception e) {
21 e.printStackTrace();
22 }
23
24 // Using character streams
25 try (Reader reader = new FileReader("input.txt");
26 Writer writer = new FileWriter("output_char_uppercase.txt")) {
27
28 int data;
29 while ((data = reader.read()) != -1) {
30 writer.write(Character.toUpperCase((char) data));
31 }
32 } catch (Exception e) {
33 e.printStackTrace();
34 }
35 }
36}
Output
Uppercase conversion using byte stream completed.
Uppercase conversion using character stream completed.

Tip

Always handle exceptions properly to ensure that your program doesn't crash unexpectedly.

Summary

ConceptDescription
InputStreamAbstract class for reading bytes from a source.
OutputStreamAbstract class for writing bytes to a destination.
Byte StreamsHandle raw binary data.
Character StreamsHandle text data encoded in specific character sets.
  • Use byte streams for binary data and character streams for text data.
  • Always close streams after use to free up system resources.
  • Proper exception handling is crucial for robust I/O operations.

What's Next?

In the next tutorial, we'll explore FileInputStream and FileOutputStream in more detail. These classes are essential for reading from and writing to files using byte streams. We'll cover their usage, methods, and practical examples to help you master file handling in Java. Stay tuned!


PreviousJava Delete FilesNext FileInputStream & FileOutputStream

Recommended Gear

Java Delete FilesFileInputStream & FileOutputStream