Java Input/Output (I/O)

Java Input/Output (I/O)

Java provides a robust I/O package (java.io) that allows you to perform input and output (I/O) operations, such as reading and writing data to files, networks, or other devices. The I/O in Java is primarily based on Streams, where a stream is a sequence of data.

Definition:

Java I/O involves handling input and output operations between the user, files, or other data sources and destinations. It utilizes streams for this purpose, which can be categorized as:
        ● Input Stream: Reads data from a source (keyboard, file, etc.).
        ● Output Stream: Writes data to a destination (file, console, etc.).
Two Types of Streams:
     1. Byte Streams: Handle binary data (8-bit data), used for reading and writing binary files.
           ○ Classes: InputStream, OutputStream, FileInputStream, FileOutputStream, etc.
     2. Character Streams: Handle character data (16-bit data), used for reading and writing text files.
           ○ Classes: Reader, Writer, FileReader, FileWriter, etc.

Common Java I/O Classes:
  • File: Represents a file or directory.
  • FileInputStream: Reads raw bytes from a file.
  • FileOutputStream: Writes raw bytes to a file.
  • FileReader: Reads characters from a file.
  • FileWriter: Writes characters to a file.
  • BufferedReader: Efficiently reads text from an input stream.
  • BufferedWriter: Efficiently writes text to an output stream.
  • Scanner: A utility class for parsing primitive types and strings using regular expressions.
Java I/O Syntax and Examples:

1. Reading from a File using FileReader and BufferedReader:

import java.io.*;

public class FileReaderExample {
    public static void main(String[] args) {
        try {
            FileReader file = new FileReader(“input.txt”);
            BufferedReader reader = new BufferedReader(file);

            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2. Writing to a File using FileWriter and BufferedWriter:

import java.io.*;

public class FileWriterExample {
    public static void main(String[] args) {
        try {
            FileWriter file = new FileWriter(“output.txt”);
            BufferedWriter writer = new BufferedWriter(file);

            writer.write(“Hello, World!”);
            writer.newLine(); // Writing a new line
            writer.write(“Welcome to Java I/O.”);

            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3. Reading Input from Console using Scanner:

import java.util.Scanner;

public class ScannerExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println(“Enter your name: “);
        String name = scanner.nextLine();

        System.out.println(“Hello, ” + name + “!”);
    }
}

4. Copying a File using FileInputStream and FileOutputStream:

import java.io.*;

public class FileCopyExample {
    public static void main(String[] args) {
        try {
            FileInputStream input = new FileInputStream(“input.txt”);
            FileOutputStream output = new FileOutputStream(“output.txt”);

            int data;
            while ((data = input.read()) != -1) {
                output.write(data);
            }

            input.close();
            output.close();

            System.out.println(“File copied successfully.”);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Tasks:
1. Basic File Reading: Write a Java program to read the contents of a text file named sample.txt and print them to the console.
2. Writing to a File: Write a program that prompts the user for a message and writes it to a file named message.txt.
3. File Copy: Implement a Java program to copy the contents of one file (source.txt) to another file (destination.txt).
4. Character Count in a File: Write a program that reads a file and counts the number of characters, words, and lines in the file.
5. Append Data to a File: Write a Java program that appends user input to an existing file named log.txt.
6. Console Input and File Writing: Create a program that reads data from the console using the Scanner class and writes the input to a file named userInput.txt.
7. File Reading with Error Handling: Write a program that reads a file using BufferedReader and handles the FileNotFoundException and IOException gracefully.

Course Video

YouTube Reference :

Frequently Asked Questions

Still have a question?

Let's talk

The training covers Java I/O operations, focusing on handling input and output between users, files, and other data sources using streams.

Yes, it includes examples demonstrating how to use byte streams (InputStream, OutputStream) and character streams (Reader, Writer) for reading and writing data.

Yes, exercises involve tasks like reading from and writing to files, and processing user input to reinforce the concepts learned.

Yes, it’s designed for beginners with clear explanations and step-by-step examples.

Yes, it explains that byte streams handle binary data, while character streams handle character data, and provides guidance on when to use each type.

Yes, the training is self-paced and accessible online anytime.

Yes, it covers classes like File, FileInputStream, FileOutputStream, FileReader, and FileWriter for file operations.

Yes, this Java Input/Output training is completely free on Iqra Technology Academy’s website.