Class 11 | I/O and Java Applets | Java Programming Notes

I/O and Java Applets – Java Programming Notes

I/O and Java Applets are important topics in Java programming. Java I/O allows programs to receive input and produce output using the keyboard, console, files and other sources. Java Applets were small Java programs designed to run inside an applet-compatible environment.

In these notes, students will learn about Java input/output streams, System.in, System.out, System.err, byte streams, character streams, file handling, and Java Applet lifecycle methods with simple Java examples.

What is I/O in Java?

I/O stands for Input and Output. Java I/O provides different classes and methods that allow Java programs to receive information from a source and send information to a destination.

Input

Input means data entering the Java program. Input may come from:

  • Keyboard
  • File
  • Network
  • Memory
  • Other input devices

Output

Output means data produced by a Java program. It can be sent to:

  • Computer screen
  • File
  • Network
  • Memory
  • Other output devices

Many traditional Java input/output classes are available through the java.io package.

Java Streams

A stream is a flow of data between a source and a destination. Java uses streams to perform input and output operations.

Source → Input Stream → Java Program → Output Stream → Destination

Standard Streams in Java

Java provides three commonly used standard streams:

Stream Purpose
System.in Standard input stream
System.out Standard output stream
System.err Standard error output stream

1. System.in

System.in is normally used to receive input from the keyboard. It is an InputStream.

Java Example – System.in
import java.io.IOException;

public class SystemInExample {

    public static void main(String[] args) throws IOException {

        System.out.println("Enter a character:");

        int data = System.in.read();

        System.out.println("You entered: " + (char)data);
        System.out.println("Character value: " + data);
    }
}

Some methods associated with input streams include:

  • read()
  • read(byte[] b)
  • available()
  • close()

Taking Input Using Scanner

For beginners, Java’s Scanner class provides a convenient way of taking keyboard input.

Java Example – Scanner
import java.util.Scanner;

public class StudentInput {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.print("Enter your name: ");
        String name = input.nextLine();

        System.out.print("Enter your age: ");
        int age = input.nextInt();

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);

        input.close();
    }
}

2. System.out

System.out is used to display normal output on the computer screen.

Common methods are:

  • print()
  • println()
  • printf()

print()

The print() method prints output without automatically moving the cursor to the next line.

Java Example – print()
public class PrintExample {

    public static void main(String[] args) {

        System.out.print("Nepal ");
        System.out.print("eNotes");
    }
}

Output:

Nepal eNotes

println()

The println() method prints the output and then moves the cursor to the next line.

Java Example – println()
public class PrintlnExample {

    public static void main(String[] args) {

        System.out.println("Java");
        System.out.println("Programming");
    }
}

Output:

Java
Programming

printf()

The printf() method is useful when formatted output is required.

Java Example – printf()
public class PrintfExample {

    public static void main(String[] args) {

        int marks = 85;
        double percentage = 85.4567;

        System.out.printf("Marks = %d%n", marks);
        System.out.printf("Percentage = %.2f%%%n", percentage);
    }
}

Output:

Marks = 85
Percentage = 85.46%

3. System.err

System.err is primarily used to display error or diagnostic messages.

Java Example – System.err
public class ErrorExample {

    public static void main(String[] args) {

        System.err.println("Error: Invalid input.");
    }
}

Types of Streams

Streams can first be divided according to direction:

  1. Input Stream
  2. Output Stream

Input Stream

Input streams read data from a source and transfer it into a Java program.

Examples:

  • FileInputStream
  • BufferedInputStream
  • DataInputStream
  • ByteArrayInputStream

Output Stream

Output streams transfer data from a Java program to a destination.

Examples:

  • FileOutputStream
  • BufferedOutputStream
  • DataOutputStream
  • ByteArrayOutputStream

Byte Streams

Byte streams work mainly with raw 8-bit bytes. They are useful for working with binary data.

Examples of binary data include:

  • Images
  • Audio files
  • Video files
  • Binary documents

The major base classes are:

  • InputStream
  • OutputStream

Common Byte Stream Classes

Class Purpose
FileInputStream Reads bytes from a file
FileOutputStream Writes bytes into a file
BufferedInputStream Provides buffered byte input
BufferedOutputStream Provides buffered byte output
DataInputStream Reads Java primitive data types
DataOutputStream Writes Java primitive data types

Copying a File Using Byte Stream

Java Example – FileInputStream and FileOutputStream
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileCopy {

    public static void main(String[] args) throws IOException {

        FileInputStream input = null;
        FileOutputStream output = null;

        try {

            input = new FileInputStream("source.txt");
            output = new FileOutputStream("destination.txt");

            int data;

            while ((data = input.read()) != -1) {

                output.write(data);
            }

            System.out.println("File copied successfully.");
        }

        finally {

            if (input != null)
                input.close();

            if (output != null)
                output.close();
        }
    }
}

Character Streams

Character streams are mainly designed for text data. They work with characters instead of raw byte-oriented data.

The main abstract classes are:

  • Reader
  • Writer

Common Character Stream Classes

Class Purpose
FileReader Reads characters from a text file
FileWriter Writes characters into a text file
BufferedReader Provides buffered character input
BufferedWriter Provides buffered character output
InputStreamReader Converts bytes into characters
OutputStreamWriter Converts characters into bytes
PrintWriter Provides convenient text output methods

Reading a Text File

Java Example – FileReader
import java.io.FileReader;
import java.io.IOException;

public class ReadTextFile {

    public static void main(String[] args) throws IOException {

        FileReader reader = null;

        try {

            reader = new FileReader("notes.txt");

            int data;

            while ((data = reader.read()) != -1) {

                System.out.print((char)data);
            }
        }

        finally {

            if (reader != null)
                reader.close();
        }
    }
}

Byte Stream vs Character Stream

Byte Stream Character Stream
Works mainly with bytes Works with characters
Suitable for binary data Suitable for text data
Uses InputStream and OutputStream Uses Reader and Writer
Can handle images and audio Designed for textual information
Example: FileInputStream Example: FileReader

File Handling in Java

File handling allows Java programs to save information permanently and retrieve that information later.

A Java program can perform operations such as:

  • Create files
  • Write data
  • Read data
  • Append information
  • Check file properties
  • Delete files

Writing Data to a File

Java Example – FileWriter
import java.io.FileWriter;
import java.io.IOException;

public class WriteFile {

    public static void main(String[] args) throws IOException {

        FileWriter writer =
            new FileWriter("student.txt");

        writer.write(
            "Welcome to Java Programming"
        );

        writer.close();

        System.out.println(
            "Data written successfully."
        );
    }
}

Reading Data from a File

Java Example – Reading File
import java.io.FileReader;
import java.io.IOException;

public class ReadFile {

    public static void main(String[] args) throws IOException {

        FileReader reader =
            new FileReader("student.txt");

        int data;

        while ((data = reader.read()) != -1) {

            System.out.print((char)data);
        }

        reader.close();
    }
}

BufferedReader Example

Java Example – BufferedReader
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class BufferedInputExample {

    public static void main(String[] args)
        throws IOException {

        BufferedReader reader =
            new BufferedReader(
                new InputStreamReader(System.in)
            );

        System.out.print("Enter your name: ");

        String name = reader.readLine();

        System.out.println(
            "Welcome, " + name
        );
    }
}

What is a Java Applet?

A Java Applet is a small Java program that was historically designed to execute within a Java-compatible browser environment or an Applet Viewer.

Unlike a normal Java application, a traditional applet normally did not start through a standard main() method. Instead, its execution was controlled using lifecycle methods.

Important: Java Applets are legacy technology. Modern mainstream web browsers no longer support traditional Java browser plug-ins. Applets are therefore mainly studied today for academic and historical understanding.

Java Application vs Java Applet

Java Application Java Applet
Standalone program Historically executed inside an applet environment
Usually starts using main() Uses applet lifecycle methods
Runs directly using JVM Traditionally required Applet Viewer or compatible browser technology
Used for general Java programs Mostly studied as legacy Java technology

Java Applet Life Cycle

The important traditional Applet methods are:

  1. init()
  2. start()
  3. paint()
  4. stop()
  5. destroy()
init() → start() → paint() → stop() → destroy()

1. init()

The init() method performs initial setup when the applet is loaded.

public void init() {

    // Initialization code
}

2. start()

The start() method runs when the applet becomes active.

public void start() {

    // Start applet operations
}

3. paint()

The paint() method is traditionally used for displaying text and graphics.

public void paint(Graphics g) {

    g.drawString(
        "Welcome to Java Applet",
        50,
        50
    );
}

4. stop()

The stop() method is called when the applet becomes inactive.

public void stop() {

    // Stop temporary operations
}

5. destroy()

The destroy() method is used for final cleanup before the applet is permanently removed.

public void destroy() {

    // Final cleanup
}

Complete Java Applet Example

Java Program – Simple Applet
import java.applet.Applet;
import java.awt.Graphics;

public class MyApplet extends Applet {

    public void init() {

        // Initialization
    }

    public void start() {

        // Applet starts
    }

    public void paint(Graphics g) {

        g.drawString(
            "Welcome to Java Applet",
            50,
            50
        );
    }

    public void stop() {

        // Applet stops
    }

    public void destroy() {

        // Cleanup
    }
}

Drawing Graphics in Java Applet

Traditional Java Applets could use the Graphics class to draw different shapes.

Common methods include:

  • drawString()
  • drawLine()
  • drawRect()
  • fillRect()
  • drawOval()
  • fillOval()
Java Applet Graphics Example
import java.applet.Applet;
import java.awt.Graphics;

public class GraphicsApplet extends Applet {

    public void paint(Graphics g) {

        g.drawString(
            "Java Graphics",
            50,
            40
        );

        g.drawLine(
            50,
            60,
            220,
            60
        );

        g.drawRect(
            50,
            80,
            120,
            60
        );

        g.drawOval(
            50,
            170,
            120,
            60
        );
    }
}

Important Points to Remember

  • I/O means Input and Output.
  • java.io contains many traditional Java I/O classes.
  • System.in is used for standard input.
  • System.out is used for normal standard output.
  • System.err is used for error output.
  • Byte streams are suitable for binary information.
  • Character streams are useful for text.
  • FileInputStream reads bytes from files.
  • FileOutputStream writes bytes to files.
  • FileReader reads character data.
  • FileWriter writes character data.
  • The major traditional Applet lifecycle methods are init(), start(), paint(), stop() and destroy().
  • Java Applets are no longer supported by modern mainstream browsers.

Important Exam Questions

  1. What is Input and Output in Java?
  2. Define a stream in Java.
  3. What is System.in?
  4. Explain System.out.
  5. What is System.err?
  6. Differentiate between print() and println().
  7. Explain printf() with an example.
  8. What is an input stream?
  9. What is an output stream?
  10. What is a byte stream?
  11. What is a character stream?
  12. Differentiate between byte stream and character stream.
  13. What is FileInputStream?
  14. What is FileOutputStream?
  15. Explain FileReader and FileWriter.
  16. What is BufferedReader?
  17. Write a Java program to read data from a file.
  18. Write a Java program to write data into a file.
  19. What is a Java Applet?
  20. Differentiate between Java Application and Java Applet.
  21. Explain the life cycle of a Java Applet.
  22. What is the purpose of init()?
  23. What is the purpose of paint()?
  24. Explain start(), stop() and destroy().
  25. Write a simple Java Applet program.

Frequently Asked Questions

What is Java I/O? Java I/O is the mechanism through which Java programs read data from sources and write data to destinations.
What are the standard streams in Java? The three common standard streams are System.in, System.out and System.err.
What is the difference between byte and character streams? Byte streams are mainly used for raw byte-oriented or binary data, while character streams are designed for text and characters.
Which Java classes can read and write files? Common classes include FileInputStream, FileOutputStream, FileReader and FileWriter.
What is a Java Applet? A Java Applet is a small Java program historically designed to execute in an applet-compatible browser environment or Applet Viewer.
Are Java Applets still used in modern websites? Traditional Java Applets are now legacy technology and are not supported by modern mainstream web browsers.
What are the main Java Applet lifecycle methods? The traditional lifecycle methods are init(), start(), paint(), stop() and destroy().

Final Words

Java I/O is an essential part of Java programming because it allows applications to communicate with keyboards, files and other sources using input and output streams.

Students should understand the difference between byte streams and character streams and become familiar with classes such as FileInputStream, FileOutputStream, FileReader, FileWriter and BufferedReader.

Java Applets are mainly an academic and historical topic today, but learning their structure and lifecycle provides useful knowledge about earlier Java GUI and web-programming concepts.

Use these I/O and Java Applets Notes for study, revision, practical programming and examination preparation.

Discussion

Share a helpful question, idea, or explanation with other students.

Leave a Comment

Write a clear question, answer, or helpful explanation.
Your email will not be published.

Download Our Offline App

Study class-wise notes even when internet is not available. Get the app from Play Store.

Nepal eNotes offline app preview
Get it on Google Play