πŸŽ‰ Special Offer !    Code: GET300OFF    Flat β‚Ή300 OFF on every Java Course
Grab Deal πŸš€

Java Input Output (I/O) Introduction  


Introduction
  • Input/Output (I/O) in Java is the process through which a Java application receives data (input) from various sources such as keyboard, files or network and then produces data (output) to different destinations such as console, files, printers, network or other devices.
  • Java Input Output
  • Use of I/O in Projects:
    1. File Handling
      • Reading/writing configuration files, logs, reports, and documents.
      • Example: Writing log files in enterprise applications.
    2. Web Applications
      • Handling file uploads/downloads (images, PDFs, documents).
      • Example: Uploading profile pictures in a web portal.
    3. Networking Applications
      • Sending and receiving data over sockets using I/O streams.
      • Example: Chat applications, client-server communication.
    4. Media Streaming
      • Handling byte streams for audio, video, and image processing.
      • Example: Video conferencing, music streaming apps.
    5. APIs and Microservices
      • Reading/writing JSON, XML, or text data when communicating between services.
    6. Logging Frameworks
      • Writing application logs (error logs, audit logs) to files or monitoring systems.
  • In Java, data is transferred in the form of streams.
    • A stream is simply a sequence of data that flows from a source to a destination.
    • Think of it like a pipeline through which data moves, one piece after another.
    • Streams make it easy to handle data whether it comes from a file, keyboard, network, or any other input/output device.
    • Java provides two types of streams to support all types of data:
      1. Byte Streams – used for handling binary data such as images, audio, video and other non-text files.
      2. Character Streams – used for handling textual data such as .txt, .java, .xml and other Unicode-based files.

Predefined Streams in Java:
  • Java provides three predefined I/O streams that are available in every program. They are part of the System class.
    1. System.in
      • Type: InputStream (specifically BufferedInputStream by default).
      • Description: Standard input stream used to take input from the keyboard.
      • Example: int data = System.in.read(); // reads a single byte
    2. System.out
      • Type: PrintStream (a subclass of OutputStream).
      • Description: Standard output stream used to print data to the console.
      • Example: System.out.println("Hello World");
    3. System.err
      • Type: PrintStream (same as System.out, but meant for errors).
      • Description: Standard error stream used to print error messages separately from normal output.
      • Example: System.err.println("This is an error message");
  • By default, these three streams are for console (standard input/output) only.
    • But Java allows us to redirect them to files (or other streams):

Java I/O Packages:
  • To handle file handling operations, Java provides two main packages:
    1. java.io Package
      • Purpose:
        • The java.io package provides stream-based I/O for reading and writing data (bytes or characters) sequentially.
      • Type of Streams:
        • Byte Streams: InputStream, OutputStream (read/write bytes)
        • Character Streams: Reader, Writer (read/write characters)
      • Common Classes:
        • FileInputStream, FileOutputStream – Read/write bytes from/to files
        • FileReader, FileWriter – Read/write text files
        • BufferedInputStream, BufferedOutputStream, BufferedReader, BufferedWriter – Buffered streams for efficiency
        • DataInputStream, DataOutputStream – Read/write primitive data types
        • ObjectInputStream, ObjectOutputStream – Read/write objects (serialization)
        • PrintWriter – Write formatted text
      • Advantages:
        • Simple to use for basic file operations
        • Suitable for small files or sequential processing
      • When to Use:
        • When working with simple text or binary files
        • When performance is not critical for large-scale data
    2. java.nio Package
      • Purpose:
        • The java.nio package provides buffer-oriented, non-blocking I/O for high-performance file handling.
      • Core Concepts:
        • Buffers: Containers for data, like arrays
        • Channels: Connect buffers to files or sockets
        • Selectors: Used for non-blocking I/O
      • Common Classes & Interfaces:
        • FileChannel – Read/write files using channels
        • ByteBuffer, CharBuffer – Buffers for data storage
        • Path, Paths, Files – Modern file operations (create, delete, copy, move)
        • StandardOpenOption – Specify options when opening files
        • Charset – Encoding/decoding for character data
      • Advantages:
        • Non-blocking I/O allows multiple operations concurrently
        • High performance for large files and network operations
        • Memory-mapped files for fast access
      • When to Use:
        • When working with large files
        • When high-performance I/O is needed
        • When doing network programming with channels

Important Terms in Java I/O:
  • Stream: A stream represents a flow of data from a source to a destination.
    • Input Stream: Used for reading data (data coming into the program).
    • Output Stream: Used for writing data (data going out of the program).
  • Input:
    • The process of reading data from a source such as keyboard, file, or network.
  • Output:
    • The process of writing data to a destination such as console, file, or printer.
  • File Handling:
    • The process of managing data stored in files, including operations like reading, writing, updating, and deleting files.
  • Reader / Writer:
    • These are classes designed specifically for handling character-based (text) data.
  • InputStream / OutputStream:
    • These are classes designed for handling byte-based (binary) data such as images, audio, and video.
  • Buffering:
    • A technique used to improve I/O performance by temporarily storing data in memory before processing.
  • Serialization:
    • The process of converting an object into a byte stream for storage or transmission.
  • Deserialization:
    • The process of converting a byte stream back into an object.
  • NIO (New I/O):
    • Introduced in Java 1.4, it provides more powerful and flexible I/O features such as channels, selectors and buffers for faster and non-blocking data handling.